MongoDB: number of matching nested array elements

I have a simple parent child saved as a document in MongoDB. Something simplistic like Order / OrderItems. (Order has an array of OrderItem)

What I would like to do is query for the number of order items, where they match a set of criteria.

Example: In order "999", find out how many order items had a quantity of 3.

db.collection.find( {OrderId:999, "OrderItems.QuantityOrdered":3} ).count();

How this query works, it returns "1", because if it matches at least one OrderItem element inside the array, it returns the number of agreed orders.

How can I ask how many "OrderItems" match ?:

+3
source share
1 answer

. .

, map-reduce, .

+3

Source: https://habr.com/ru/post/1606618/


All Articles