【发布时间】:2019-01-12 21:34:07
【问题描述】:
假设我们在 mongodb 集合中有这样的对象:
{
_id: 00000001
colors: ["green", "yellow"],
houses: [
{
number: 1,
owner: "John"
},
{
number: 2,
owner: "John"
},
{
number:3,
owner: "Dave"
}
]
},
{
_id: 00000002
colors: ["green", "red"],
houses: [
{
number: 15,
owner: "Dave"
},
]
}
因此,要获取颜色数组包含绿色的每个对象,我需要编写的查询看起来像这样:collection.find({colors: "green"});
现在,如果我想获取 John 拥有房屋的所有对象,我将如何制定这样的查询?
基本上我要问的是,如果我的查询是collection.find({houses: {owner: "John", number: ?}}),我需要用什么替换"?" 来告诉mongo我不在乎数字有什么值。
或者也许还有另一种我没有想到的方法?
感谢您的任何帮助!
(顺便说一句,这是一个虚构的例子,因此为什么 ID 看起来很奇怪,而对象本身似乎不是很有用。)
【问题讨论】: