查询父文档数组中的子文档 [英] Query sub documents in array of parent document
本文介绍了查询父文档数组中的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
db.animals
db.animals
{'animal':'monkey',
'quantity:3
'id' : 001}
{'animal':'pig',
'quantity:5
'id' : 002}
{'animal':'cow',
'quantity:3
'id' : 003}
db.people
db.people
{'person':'john',
'have':[001, 003]} // objectId
我如何获得约翰的动物?我希望这样的结果:
How can I get animals of John?. I expect the result like this:
[{'animal':'monkey',
'quantity:3
'id' : 001},
{'animal':'cow',
'quantity:3
'id' : 003}]
推荐答案
MongoDB中没有联接,因此您必须分两个步骤进行操作.
There are no joins in MongoDB so you have to do it in two steps.
在外壳中:
var john = db.people.findOne({person: 'john'})
db.animals.find({id: {$in: john.have}})
这篇关于查询父文档数组中的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文