Q: How to calcula
The last of the or
The present invent
A/B Testing - na
A high dose of a n
Wir stellen unsere
--- title: "Compil
/* * This progra
Novel drug resista
1. Introduction ==

The invention rela
Q: Why does this
The first-ever dra
Ceramic is a commo
In the present sta

Bacteriophage T4 h
#pragma once #incl
1. Field of the In
Q: How to use .cl
Q: Mongodb find documents by number of items in array I want to be able to query for documents that have an array of more than n number of items Say we have an array, which has one element: { array: [ { _id: 1, name: "first" }, { _id: 2, name: "second" }, { _id: 3, name: "third" } ] } Which I want to find if there are three items in the array. I want to do this without updating the documents. Is there a way to do this via some sort of query that would check that array's length is 3? A: You can use $size on array field in order to achieve this. db.collection.find({'array.$size': {'$gte': 3}}) A: It is not possible. All indexes are sparse. Queries only check "leaf values" (the actual values in the documents, not the whole value), and only in the "projection" part of the query. So in your case, the only place "array.$size" could ever be used, would be in the projection part of the query. And there it does not make sense, because there it simply evaluates to 1 (or more if there are multiple elements in array). A: You can use below aggregation. db.collection.aggregate({$project:{"array":{ $size: "$array" }}}).forEach(function(doc) { printjson(doc) }) or db.collection.find({ array:{ $size: 3 }}) This will output documents having array size is greater than 3. You need to change above query based on your need NOTE : $size is not optimized for array having more than 64000 elements, may be in later releases this limitation will be removed. Ref : http://docs.mongodb.org/manual/reference/operator/size/ EDIT: 1 After update to the question. db.coll.find({array:{$size: {$gt:3}}}) This query will return documents having array size more than 3. It's working perfectly, I also verified it with Mongotest.org db.test.insert({ "_id" : 1, "array" : [ { "key" : 1 }, { "key" : 2 } ] }) db.test.insert({ "_id" : 2, "array" : [ { "key" : 3 }, { "key" : 4 } ] }) db.test.insert({ "_id" : 3, "array" : [ { "key" : 5 }, { "key" : 6 } ] }) db.test.insert({ "_id" : 4, "array" : [ { "key" : 1 }, { "key" : 2 }, { "key" : 3 } ] }) db.test.insert({ "_id" : 5, "array" : [ { "key" : 7 }, { "key" : 8 } ] }) db.test.find() { "_id" : 5, "array" : [ { "key" : 7 }, { "key" : 8 } ] } db.test.find({array:{$size: {$gt:3}}}) { "_id" : 4, "array" : [ { "key" : 1 }, { "key" : 2 }, { "key" : 3 } ] }