简体   繁体   English

无法使用Java执行mongo db聚合查询,

[英]Not able to execute the mongo db aggregate query using java,

I am working on writing aggregate queries using java for mongo db for first time. 我正在首次使用Java为mongo db编写聚合查询。 I am not able to convert the shell query which I wrote previously to java format. 我无法将之前编写的Shell查询转换为Java格式。 I am facing some issues. 我面临一些问题。 The below is the shell query which I wrote already and it's working fine. 下面是我已经写过的shell查询,它工作正常。

Date set for rooms. 为房间设置的日期。

{
 "_id": ObjectId("571c5724db62826826d28d08"),
"conversationId": "6puebew70kke29",
"userId": "600",
"firstName": "Test",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.753Z"),
"__v": 0
}
{
   "_id": ObjectId("571c5724db62826826d28d09"),
"conversationId": "6puebew70kke29",
"userId": "900",
"firstName": "User",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.754Z"),
"__v": 0
 }

  {
"_id": ObjectId("571c574edb62826826d28d0b"),
"conversationId": "fsny11z742kpgb9",
"userId": "600",
"firstName": "FitTest",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.192Z"),
"__v": 0
 }

  {
"_id": ObjectId("571c574edb62826826d28d0c"),
"conversationId": "fsny11z742kpgb9",
"userId": "800",
"firstName": "Dev",
"profileImagePath": "",
  "created": ISODate("2016-04-24T05:19:10.193Z"),
"__v": 0
    }



 rooms.aggregate([{
        $match: {
            type: 'PRIVATE'
        }
    }, {
        $group: {
            _id: '$conversationId',
            users: {
                $push: '$userId'
            }
        }
    }, {
        $match: {
            users: {
                $all: [friendProfileData.id, userprofileData.id]
            }
        }
    }, ]

Java code for the above query. 以上查询的Java代码。

 Aggregation agg = newAggregation(
            match(Criteria.where("type").is("PRIVATE")),
            group("_id", "conversationId"),
            group("users").push("userId").as("users")
            );

    AggregationResults<Rooms> groupResults = mongoOps.aggregate(agg,   "rooms", Rooms.class);
    List<Rooms> result = groupResults.getMappedResults();

Not able to complete it fully still I am not aware how to write few expressions. 无法完全完成它,我仍然不知道如何编写一些表达式。 Your help is appreciated. 感谢您的帮助。

I have made an assumption as I am not sure about "friendProfileData" and "userprofileData" attributes in the last "match". 我做了一个假设,因为我不确定最后一个“匹配”中的“ friendProfileData”和“ userprofileData”属性。

You can change the "Filters.all" statement accordingly as per your requirement. 您可以根据需要更改“ Filters.all”语句。 Otherwise, this code should meet your requirement and it works fine with "MongoDB 3.2.0" and "Mongo Java Driver 3.2.2 Jar". 否则,此代码应满足您的要求,并且可以与“ MongoDB 3.2.0”和“ Mongo Java Driver 3.2.2 Jar”一起正常工作。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;

public static void main(String[] args) {
        MongoClient client = new MongoClient();
        MongoDatabase database = client.getDatabase("localhost");

        List<Bson> aggregateList = new ArrayList<>();

        aggregateList.add(Aggregates.match(Filters.eq("type", "PRIVATE")));
        aggregateList.add(Aggregates.group("$conversationId", Accumulators.push("users", "$userId")));
        aggregateList.add(Aggregates.match(Filters.all("users", Arrays.asList("1", "800"))));

        AggregateIterable<Document> mongoCollectionList = database.getCollection("rooms")
                .aggregate(aggregateList);

        MongoCursor<Document> mongoCursor = mongoCollectionList.iterator();

        while (mongoCursor.hasNext()) {
            System.out.println(mongoCursor.next());

        }

    }

Maven Dependency:- Maven的依赖:

The above works fine with the below Maven dependency. 上面的工作与下面的Maven依赖关系很好

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.2.2</version>
</dependency>

Please note that the above may not work if you use the below jar:- 请注意,如果您使用以下罐子,则上述方法可能无法使用:-

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver</artifactId>
    <version>3.2.2</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM