gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:45:21 25 4
gpt4 key购买 nike

我正在第一次使用 java 为 mongo db 编写聚合查询。我无法将我之前编写的 shell 查询转换为 java 格式。我面临一些问题。下面是我已经编写的 shell 查询,它工作正常。

房间日期设定。

{
"_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 代码。

 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();

无法完全完成它,但我不知道如何编写一些表达式。感谢您的帮助。

最佳答案

我做了一个假设,因为我不确定最后一个“匹配”中的“friendProfileData”和“userprofileData”属性。

您可以根据您的要求相应地更改“Filters.all”语句。否则,此代码应该满足您的要求,并且它可以与“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 依赖项:-

上面的工作正常与下面的 Maven 依赖项。

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

请注意,如果您使用以下 jar,则上述可能不起作用:-

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

关于java - 无法使用java执行mongo db聚合查询,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com