यह जांचने के लिए कि क्या MongoDB में कोई फ़ील्ड [] या {} है, आप निम्न सिंटैक्स का उपयोग कर सकते हैं -
db.yourCollectionName.find({
"yourOuterFieldName": { "$gt": {} },
"yourOuterFieldName.0": { "$exists": false }
}); आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.checkFieldDemo.insert([
... { _id: 1010, StudentDetails: {} },
... { _id: 1011, StudentDetails: [ { StudentId: 1 } ] },
... { _id: 1012, StudentDetails: [ {} ] },
... { _id: 1013 },
... { _id: 1014, StudentDetails: null},
... { _id: 1015, StudentDetails: { StudentId: 1 } }
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}) खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.checkFieldDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : 1010, "StudentDetails" : { } }
{ "_id" : 1011, "StudentDetails" : [ { "StudentId" : 1 } ] }
{ "_id" : 1012, "StudentDetails" : [ { } ] }
{ "_id" : 1013 }
{ "_id" : 1014, "StudentDetails" : null }
{ "_id" : 1015, "StudentDetails" : { "StudentId" : 1 } } MongoDB में कोई फ़ील्ड [] या {} −
. है या नहीं, यह जांचने के लिए क्वेरी निम्नलिखित है> db.checkFieldDemo.find({
... "StudentDetails": { "$gt": {} },
... "StudentDetails.0": { "$exists": false }
... }); यह निम्नलिखित आउटपुट देगा -
{ "_id" : 1015, "StudentDetails" : { "StudentId" : 1 } }