用Mongo查询数组元素 [英] Querying array elements with Mongo

查看:85
本文介绍了用Mongo查询数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查询其中装有苹果的冰沙? (下面是一个包含3个文档的集合)

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

    )

推荐答案

如果仅运行以下查询,MongoDB足够聪明,可以弄清楚您要做什么.

If you simply run the below query MongoDB is smart enough to figure out what you're trying to do.

{ ingredients: "apple" }

Mongo将看到成分是一个列表,并且仅返回列表中某些位置包含"apple"的文档.

Mongo will see that ingredients is a list and only return documents that contain "apple" some where in that list.

这篇关于用Mongo查询数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆