MongoDB query based on count of embedded document

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

    I too faced this dilemma. Not sure why it doesnt exist in MongoDB by default. The most efficient way is to also store a property called count or length or something that denotes how many items are in the array.

    Then you can index that property and do range queries on it. It'll keep your queries simple and quick.

    Just make sure that your application keeps it in sync.

提交回复
热议问题