如何查询含有苹果的冰沙? (下面是一个包含 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
)
最佳答案
如果您只是简单地运行以下查询,MongoDB 就足够聪明,可以弄清楚您要做什么。
{ ingredients: "apple" }
Mongo 将看到成分是一个列表,并且只返回该列表中某些位置包含“苹果”的文档。
关于mongodb - 使用 Mongo 查询数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3308119/