You can use the $ where operator.
> db.orders.save({Order: {items: [1,2]}}) > db.orders.save({Order: {items: [1,2,3]}}) > db.orders.find({$where:function() { if (this["Order"]["items"].length > 2) return true; }}) { "_id" : ObjectId("4d334c9102bcfe450ce52585"), "Order" : { "items" : [ 1, 2, 3 ] } }
There are two drawbacks to $, where they cannot use the index, and the BSON object must be converted to a JavaScript object, since you are executing your own JavaScript function for each document in the collection.
So $ where for large collections can be very slow. But for special or rarely executed queries, this is very convenient. If you often need to run such queries, you should follow the Bugai13 recommendation, as you can index the key in which you store the size of the array.
source share