Find MongoDB records where array field is not empty Find MongoDB records where array field is not empty mongoose mongoose

Find MongoDB records where array field is not empty


If you also have documents that don't have the key, you can use:

ME.find({ pictures: { $exists: true, $not: {$size: 0} } })

MongoDB don't use indexes if $size is involved, so here is a better solution:

ME.find({ pictures: { $exists: true, $ne: [] } })

If your property can have invalid values (like null boolean or others) , then you an add an additional check using $types as proposed in this answer:

With mongo >= 3.2:

ME.find({ pictures: { $exists: true, $type: 'array', $ne: [] } })

With mongo < 3.2:

ME.find({ pictures: { $exists: true, $type: 4, $ne: [] } })

Since MongoDB 2.6 release, you can compare with the operator $gt but could lead to unexpected results (you can find a detailled explanation in this answer):

ME.find({ pictures: { $gt: [] } })


After some more looking, especially in the mongodb documents, and puzzling bits together, this was the answer:

ME.find({pictures: {$exists: true, $not: {$size: 0}}})


This might also work for you:

ME.find({'pictures.0': {$exists: true}});