MongoDB query based on count of embedded document

前端 未结 4 389
一生所求
一生所求 2021-01-03 05:15

Suppose I have:

Order: {_id: ..., items: [...]}

How to filter orders which have item number greater than 5?

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 05:29

    To see if the length is greater than 5, you can test if a 6th element exists:

    db.orders.find({"items.6": {$exists: true}})
    

    This is much more efficient than using $where.

提交回复
热议问题