How to check if a field in MongoDB is [] or {}? -


mongodb. 1 of fields of document can either array, including empty array, subdocument, can empty or not, or null, or not exist @ all. need condition find() match non-empty subdocument, , that.

so:

fieldname: {} - no match. fieldname: [ { id:0 } ] - no match. fieldname: [ {} ] - no match. no field called fieldname - no match. fieldname: null - no match. fieldname: { id: 0 } - match. 

i have no rights modify anything, have work database is. how formulate find() ?

use following query:

db.test.find({     "fieldname": { "$gt": {} },     "fieldname.0": { "$exists": false }  }) 

for example, above test case, insert following documents:

db.test.insert([     { _id: 1, fieldname: {} },     { _id: 2, fieldname: [ { id: 0 } ] },     { _id: 3, fieldname: [ {} ] },     { _id: 4 },     { _id: 5, fieldname: null},     { _id: 6, fieldname: { id: 0 } } ]) 

the above query return document _id: 6

/* 0 */ {     "_id" : 6,     "fieldname" : {         "id" : 0     } } 

Comments