आइए दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo74.insertOne( ... { ... StudentName: "Chris", ... StudentDetails: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... },{ ... "Subject": "MongoDB", ... "isRegular": "InActive" ... },{ ... "Subject": "MongoDB", ... "isRegular": "InActive" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e29c6b671bf0181ecc4226f") } > db.demo74.insertOne({ ... name: "document2", ... data: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... },{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... },{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e29c6b771bf0181ecc42270") }
संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo74.find();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e29c6b671bf0181ecc4226f"), "StudentName" : "Chris", "StudentDetails" : [ { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "InActive" }, { "Subject" : "MongoDB", "isRegular" : "InActive" } ] } { "_id" : ObjectId("5e29c6b771bf0181ecc42270"), "name" : "document2", "data" : [ { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "Active" } ] }
निम्नलिखित दस्तावेजों को खोजने के लिए क्वेरी है जहां सरणी में सभी वस्तुओं का विशिष्ट मूल्य है -
> db.demo74.find({ " StudentDetails": { "$not": { "$elemMatch": { "isRegular": { $ne: "Active" } } } }, "StudentDetails.isRegular": "Active" }).pretty();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e29c6b671bf0181ecc4226f"), "StudentName" : "Chris", "StudentDetails" : [ { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "InActive" }, { "Subject" : "MongoDB", "isRegular" : "InActive" } ] }