你可能想試試這個模塊@coolgk/mongo,它可以讓你的加入,對多個集合進行篩選。
例子
SQL來蒙戈加入
左連接
SELECT * FROM a LEFT JOIN b ON a.b_id = b.id
成爲
model.find({}, {
join: [ { on: 'b_id' } ]
})
結果:
[{
_id: '5a8bde4ae2ead929f89f3c42',
a_name: 'aname1',
b_id: {
_id: '5a8bde4ae2ead929f89f3c41',
b_name: 'bname1'
}
}, { ... }, ... ]
內加入與約束
SELECT * FROM a, b WHERE a.b_id = b.id AND b.b_name = 'bname1'
成爲
model.find({}, {
join: [ { on: 'b_id', filters: { b_name: 'bname1' } } ]
})
結果:
[{
_id: '5a8bdfb05d44ea2a08fa8a4c',
a_name: 'aname2',
b_id: {
_id: '5a8bdfb05d44ea2a08fa8a4b',
b_name: 'bname2'
}
}]
內加入對多張收藏
SELECT * FROM a, b, c WHERE a.b_id = b.id AND b.c_id = c.id AND c.c_name = 'cname3'
modela.find({}, {
join: [{
on: 'b_id',
join: [{
on: 'c_id',
filters: { c_name: 'cname3' }
}]
}]
})
結果:
[{
_id: '5a8bdfc1b07af22a12cb1f0b',
a_name: 'aname3',
b_id: {
_id: '5a8bdfc1b07af22a12cb1f0a',
b_name: 'bname3',
c_id: {
_id: '5a8bdfc1b07af22a12cb1f09',
c_name: 'cname3'
}
}
}]
的可能的複製[?我如何執行SQL加入MongoDB中相當於(http://stackoverflow.com/questions/2350495/how-do-i-perform-the -SQL-加入等效功能於MongoDB中) – Philipp