MongoDB में नल वैल्यू क्वेरी करने के लिए $ne ऑपरेटर का उपयोग करें। पहले दस्तावेजों के साथ एक संग्रह बनाते हैं:
> db.queryingNullDemo.insertOne({
"StudentName":"Larry",
"StudentDetails": { "StudentAge":21, "StudentSubject":"MongoDB" }
});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd00bec588d4a6447b2e05f") }
> db.queryingNullDemo.insertOne({
"StudentName":"Sam",
"StudentDetails": { "StudentAge":23, "StudentSubject":null }
});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd00c00588d4a6447b2e060") }
संग्रह से सभी दस्तावेजों को find() विधि की सहायता से प्रदर्शित करने के लिए क्वेरी:
> db.queryingNullDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "_id" : ObjectId("5cd00bec588d4a6447b2e05f"), "StudentName" : "Larry", "StudentDetails" : { "StudentAge" : 21, "StudentSubject" : "MongoDB" } }
{ "_id" : ObjectId("5cd00c00588d4a6447b2e060"), "StudentName" : "Sam", "StudentDetails" : { "StudentAge" : 23, "StudentSubject" : null } }
नल के लिए क्वेरी कैसे करें:
> db.queryingNullDemo.find({'StudentDetails.StudentSubject': {$ne: null}});
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "_id" : ObjectId("5cd00bec588d4a6447b2e05f"), "StudentName" : "Larry", "StudentDetails" : { "StudentAge" : 21, "StudentSubject" : "MongoDB" } }