MongoDB में AND &OR को संयोजित करने के लिए, आइए पहले हम दस्तावेज़ों के साथ एक संग्रह बनाएँ -
>db.combinedAndOrDemo.insertOne({"StudentFirstName":"John","StudentAge":23,"StudentSkill":"MongoDB"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd306dcb64f4b851c3a13e2")
}
>db.combinedAndOrDemo.insertOne({"StudentFirstName":"Larry","StudentAge":21,"StudentSkill":"MySQL"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd306f3b64f4b851c3a13e3")
}
>db.combinedAndOrDemo.insertOne({"StudentFirstName":"Sam","StudentAge":24,"StudentSkill":"SQL Server"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd30701b64f4b851c3a13e4")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.combinedAndOrDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5cd306dcb64f4b851c3a13e2"),
"StudentFirstName" : "John",
"StudentAge" : 23,
"StudentSkill" : "MongoDB"
}
{
"_id" : ObjectId("5cd306f3b64f4b851c3a13e3"),
"StudentFirstName" : "Larry",
"StudentAge" : 21,
"StudentSkill" : "MySQL"
}
{
"_id" : ObjectId("5cd30701b64f4b851c3a13e4"),
"StudentFirstName" : "Sam",
"StudentAge" : 24,
"StudentSkill" : "SQL Server"
} संयुक्त AND &OR के लिए क्वेरी निम्नलिखित है -
> db.combinedAndOrDemo.find( {"$or":[ {"$and": [{"StudentFirstName": "John"}, {"_id": ObjectId("5cd306dcb64f4b851c3a13e2")}] }, {"StudentSkill" : "MongoDB" } ] } ); यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "_id" : ObjectId("5cd306dcb64f4b851c3a13e2"), "StudentFirstName" : "John", "StudentAge" : 23, "StudentSkill" : "MongoDB" }