MongoDB query based on count of embedded document

前端 未结 4 421
一生所求
一生所求 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:23

    You cann't query by size of embed collection, you need to create field with size of collection for such needs(mongo db documentation):

    The $size operator matches any array with the specified number of elements. The following example would match the object {a:["foo"]}, since that array has just one element:

    db.things.find( { a : { $size: 1 } } );

    You cannot use $size to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size field that you increment when you add elements.

提交回复
热议问题