gpt4 book ai didi

android - mongodb过滤与数组中objectid关联的内容状态

转载 作者:可可西里 更新时间:2023-11-01 10:37:37 25 4
gpt4 key购买 nike

我想通过过滤功能导入数据。

我认为我们应该使用“聚合”而不是“查找”。

The conditions for the data I want are:

  1. There is A Collection and B Collection.

  2. The 'contents' of A Collection points to the "_id" of B Collection.

  3. The corresponding value is only fetched when the "Visibility" value of the B Collection is "true".

“A”系列:

{
"_id" : ObjectId("aaaaa"),
"title" : "study list",
"contents" : [ ObjectId("11111"), ObjectId("22222") ]
}
{
"_id" : ObjectId("bbbbb"),
"title" : "study list",
"contents" : [ ObjectId("33333"), ObjectId("44444") ]
}
{
"_id" : ObjectId("ccccc"),
"title" : "study list",
"contents" : [ ObjectId("55555") ]
}

“B”系列:

{
"_id" : ObjectId("11111"),
"visibility" : true
}
{
"_id" : ObjectId("22222"),
"visibility" : true
}
{
"_id" : ObjectId("33333"),
"visibility" : true
}
{
"_id" : ObjectId("44444"),
"visibility" : false
}
{
"_id" : ObjectId("55555"),
"visibility" : false
}

==数据结果

{
"_id" : ObjectId("aaaaa"),
"title" : "study list",
"contents" : [ ObjectId("11111"), ObjectId("22222") ]
}

如何获取我想要的数据?

最佳答案

您可以使用以下聚合

db.collectionA.aggregate([
{ "$lookup": {
"from": "collectionB",
"localField": "contents",
"foreignField": "_id",
"as": "data"
}},
{ "$match": {
"data": {
"$not": {
"$elemMatch": { "visibility": false }
}
}
}}
])

关于android - mongodb过滤与数组中objectid关联的内容状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54016647/

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