我想通过过滤功能导入数据。
我认为我们应该使用“聚合”而不是“查找”。
我想要的数据的条件是:
有A收藏和B收藏。
A集合的“内容”指向B集合的“ _id”。
仅当B Collection的“可见性”值为“ 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") ]
}
如何获取所需数据?
答案 0 :(得分:1)
您可以使用以下汇总
db.collectionA.aggregate([
{ "$lookup": {
"from": "collectionB",
"localField": "contents",
"foreignField": "_id",
"as": "data"
}},
{ "$match": {
"data": {
"$not": {
"$elemMatch": { "visibility": false }
}
}
}}
])