使用 Mongo 查询数组元素
如何查询含有苹果的冰沙? (以下是包含3个文档的集合)
_id => 1
name => 'best smoothie'
ingredients => Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
_id => 2
name => 'summer smoothie'
ingredients => Array
(
[0] => lemon
[1] => mint
)
_id => 3
name => 'yogurt smoothie'
ingredients => Array
(
[0] => apple
[1] => blueberry
)
How can I query the smoothies that have apple in them? (below is a collection with 3 documents)
_id => 1
name => 'best smoothie'
ingredients => Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
_id => 2
name => 'summer smoothie'
ingredients => Array
(
[0] => lemon
[1] => mint
)
_id => 3
name => 'yogurt smoothie'
ingredients => Array
(
[0] => apple
[1] => blueberry
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只是运行下面的查询,MongoDB 就会足够智能地找出您想要执行的操作。
Mongo 将看到配料是一个列表,并且只返回该列表中某些位置包含“apple”的文档。
If you simply run the below query MongoDB is smart enough to figure out what you're trying to do.
Mongo will see that ingredients is a list and only return documents that contain "apple" some where in that list.
来自文档:
From the documentation:
为什么人们要为冰沙编写可扩展的应用程序?
db.find({"配料":{$in: "苹果"}});
Why do people write scalable applications for smoothies?
db.find({"ingredients":{$in: "apple"}});