无法通过查询获得 mongo 嵌入文档中的聚合结果
Not able to get the aggregate result in mongo embedded document by querying
查询 mongo 数据库中的嵌入文档时出现问题。我正在尝试为用户获取 conversationId
但它返回 null。
示例文档:
{
"_id" : ObjectId("5787391f191fda3a4430c749"),
"conversationId" : "fWFGIr0xAbQytmVcQIPV",
"user" : [{_id : "800", "name" : "Tim"},{_id : "500", "name" : "Kingsley"},
{_id : "400", "name" : "Roger"}],
"type" : "PRIVATE"
}
查询:
Aggregation agg = newAggregation(
match(Criteria.where("type").is("PRIVATE")),
group("conversationId").push("user.id").as("users"),
match(Criteria.where("users").all(Arrays.asList('800','400','500')))
);
AggregationResults<Rooms> groupResults = mongoOps.aggregate(agg, ROOMS, Rooms.class);
List<Rooms> result = groupResults.getMappedResults();
result.get(0).getId() // returns null
result.get(0).getId()
返回 null,根据我的查询,我期待为用户提供的对话 ID。
我不确定,为什么您要对 conversationId 进行分组,然后使用参与该对话的用户 ID 创建一个集合。
如果您想要的结果是查找包含用户“800”、“400”和“500”的对话,那么您可以直接使用 $all 的查找查询。
Mongo 查询将如下所示:
db.test.find({"user._id":{"$all":["800","500","400"]}})
查询 mongo 数据库中的嵌入文档时出现问题。我正在尝试为用户获取 conversationId
但它返回 null。
示例文档:
{
"_id" : ObjectId("5787391f191fda3a4430c749"),
"conversationId" : "fWFGIr0xAbQytmVcQIPV",
"user" : [{_id : "800", "name" : "Tim"},{_id : "500", "name" : "Kingsley"},
{_id : "400", "name" : "Roger"}],
"type" : "PRIVATE"
}
查询:
Aggregation agg = newAggregation(
match(Criteria.where("type").is("PRIVATE")),
group("conversationId").push("user.id").as("users"),
match(Criteria.where("users").all(Arrays.asList('800','400','500')))
);
AggregationResults<Rooms> groupResults = mongoOps.aggregate(agg, ROOMS, Rooms.class);
List<Rooms> result = groupResults.getMappedResults();
result.get(0).getId() // returns null
result.get(0).getId()
返回 null,根据我的查询,我期待为用户提供的对话 ID。
我不确定,为什么您要对 conversationId 进行分组,然后使用参与该对话的用户 ID 创建一个集合。
如果您想要的结果是查找包含用户“800”、“400”和“500”的对话,那么您可以直接使用 $all 的查找查询。
Mongo 查询将如下所示:
db.test.find({"user._id":{"$all":["800","500","400"]}})