我有2个具有以下虚拟模式的集合
字母
_id: ObjectId
title: String
status: String
测试版
_id: ObjectId
title: String
alphaId: ObjectId
alphas示例文档
db.alphas.insert([
{ "_id" : 3, "title" : "almonds", status: "approved"},
{ "_id" : 1, "title" : "pecans", status: "approved"},
])
betas示例文档
db.betas.insert([
{ "_id" : 1, "title" : "neon", "alphaId" : "1" },
{ "_id" : 2, "title" : "argon", "alphaId" : "2"},
])
我需要的是一个游标,该游标具有基于以下条件的这两个集合的文档:
[
{ "_id" : 1, "title" : "neon", "alphaId" : "1" },
{ "_id" : 2, "title" : "argon", "alphaId" : "2"},
{ "_id" : 3, "title" : "almonds", status: "approved"},
]
所以最终我可以像这样对它进行排序
cursor.sort()
在到目前为止的研究中,我找到了一种使用$ lookup在alpha
文档中填充beta
文档的方法。
db.betas.aggregate([
{
$match: {
_id: {
$exists: true
}
}
},
{
$lookup: {
from: "alphas",
localField: 'alphaId',
foreignField: '_id',
as: 'alphas'
}
},
]);
有没有办法让我在同一层上以平坦的结构同时获取这两个文件,所以我可以在整个页面上应用mongo sort
/ filter
。
注意:我可以在应用程序层上轻松实现此目的,但这将不是最佳选择。我想使用数据库层来实现这一目标。
MongoDB版本:3.3.2