PetTable {
_id: ObjectId,
bdTable: string,
status: ObjectId(ref=Status)
}
Status {
_id : ObjectId,
statusCode : number, --> exple: (1, 2, ...7)
petId : ObjectId(ref=PetTable),
}
我想用 bdTable = "zzzz" 和 status =5 找到 PetTable 的所有物件;
我不知道該怎么做?
uj5u.com熱心網友回復:
使用管道階段運行聚合操作,$lookup如下所示:
db.PetTable.aggregate([
{ $match: { bdTable: 'zzzz' } },
{ $lookup: {
from: 'Status',
let: { status_id: '$status' },
pipeline: [
{ $match: {
$expr: {
$and: [
{ $eq: [ 5, '$statusCode' ] },
{ $eq: [ '$_id', '$$status_id' ] }
]
}
} }
],
as: 'status'
} },
{ $match: {
$expr: { $gt: [ { $size: '$status' }, 0] }
} },
]);
蒙戈游樂場
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440593.html
標籤:节点.js mongodb mongodb查询 聚合框架 loopbackjs
