Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为spring boot转换mongodb的聚合查询_Java_Spring_Mongodb_Spring Data_Spring Data Mongodb - Fatal编程技术网

Java 为spring boot转换mongodb的聚合查询

Java 为spring boot转换mongodb的聚合查询,java,spring,mongodb,spring-data,spring-data-mongodb,Java,Spring,Mongodb,Spring Data,Spring Data Mongodb,我有一个mongodb查询,它可以正常工作 db.user.aggregate([ { "$project": { "data": { "$objectToArray": "$$ROOT" } } }, { $unwind: "$data" }, { "$match": {

我有一个mongodb查询,它可以正常工作

db.user.aggregate([
  {
    "$project": {
      "data": {
        "$objectToArray": "$$ROOT"
      }
    }
  },
  {
    $unwind: "$data"
  },
  {
    "$match": {
      "data.v": {
        $regex: "Mohit Chandani"
      }
    }
  }
])
它基本上是获取所有具有Mohit Chandani值的文档,以下是输出:

{ "_id" : "b387d728-1feb-45b6-bdec-dafdf22685e2", "data" : { "k" : "fullName", "v" : "Mohit Chandani" } }
{ "_id" : "8e35c497-4296-4ad9-8af6-9187dc0344f7", "data" : { "k" : "fullName", "v" : "Mohit Chandani" } }
{ "_id" : "c38b6767-6665-46b8-bd29-645c41d03850", "data" : { "k" : "fullName", "v" : "Mohit Chandani" } }
我需要为我的spring boot应用程序转换此查询,我正在编写以下内容:-

Aggregation aggregation = Aggregation.newAggregation(Aggregation.project(Aggregation.ROOT), Aggregation.match(Criteria.where(connectionRequest.getWord())));

在Spring数据中进行长聚合时,了解采用哪种方法会很有帮助。

这可能会对您有所帮助,希望您使用的是
MongoTemplate
进行聚合

@Autowired
private MongoTemplate mongoTemplate;
上面脚本的代码是

public List<Object> test() {

    Aggregation.newAggregation(
        project().and(ObjectOperators.valueOf(ROOT).toArray()).as("data"),
        unwind("data"),
        match(Criteria.where("data.v").regex("Mohit Chandani")
        )
    ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

    return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();

}

值得一提的是,您的聚合查询可能工作得很好,但我高度怀疑它的性能,因为一个字段上的regex查询有多个管道。另外,$unwind pipeline阶段往往会降低操作速度,因为在一个数组中展开所有文档需要花费一些精力。您所说的“转换为我的spring boot应用程序”是什么意思?@deadshot我需要通过spring启动应用程序执行查询,而不是从mongodb shell执行查询。@chridam是的,我明白你的意思这会有帮助的
public List<Object> test() {

    Aggregation aggregation = Aggregation.newAggregation(
        
        p-> new Document("$project",
                new Document("data",
                    new Document("$objectToArray","$$ROOT")
                )     
            ),
        unwind("data"),
        match(Criteria.where("data.v").regex("Mohit Chandani"))     

    ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

    return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();

}