mongoose不要选择与任何相关的记录
我能够成功实施查找,但问题在于;我不需要没有任何记录的记录可以加入儿童收集。目前,如果没有在儿童收藏中找到的匹配记录,我将获得空白的阵列。注意:我正在使用MongoDB灵活模式
parentCollection.aggregate([
{
'$match': {
userId: '62810c69e880ed667073add1',
type: 'data'
}
},
{
'$lookup': {
from: 'childCollection',
let: { typeId: { '$toObjectId': '$typeId' } },
pipeline: [ {
'$match': {
'$expr': { '$eq': [ '$_id', '$$typeId' ] },
'$or': [ {
'field1': { '$regex': '737', '$options': 'i' }
},
{
'field2': { '$regex': '737', '$options': 'i' }
}]
}
}],
as: 'child'
}
},
{ '$limit': 10 }, { '$skip': 0 }],
{});
I am able to successfully implement the lookup, but the problem is that; I don't need the records which doesn't have any record to join in child collection. Currently I am getting blank array for child field, if no matching record found in child collection. Note: I am using MongoDB Flexible Schema
parentCollection.aggregate([
{
'$match': {
userId: '62810c69e880ed667073add1',
type: 'data'
}
},
{
'$lookup': {
from: 'childCollection',
let: { typeId: { '$toObjectId': '$typeId' } },
pipeline: [ {
'$match': {
'$expr': { '$eq': [ '$_id', '$typeId' ] },
'$or': [ {
'field1': { '$regex': '737', '$options': 'i' }
},
{
'field2': { '$regex': '737', '$options': 'i' }
}]
}
}],
as: 'child'
}
},
{ '$limit': 10 }, { '$skip': 0 }],
{});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经通过在$查找后添加另一个$匹配来实现这一目标
I have achieved this by adding an additional $match on child after $lookup