How to find MongoDB records where array field is not empty?

Technology CommunityCategory: MongoDBHow to find MongoDB records where array field is not empty?
VietMX Staff asked 3 years ago

Consider some variants:

collection.find({ pictures: { $exists: true, $not: { $size: 0 } } }) 
collection.find({ pictures: { $exists: true, $ne: [] } })
collection.find({ pictures: { $gt: [] } }) // since MongoDB 2.6 
collection.find({'pictures.0': {$exists: true}}); // beware if performance is important - it'll do a