Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

MongoDB में $or और limit(1) से ऐरे एलिमेंट मैच करें

MongoDB में $or ऑपरेटर से ऐरे एलिमेंट मैच करके पहला डॉक्यूमेंट प्राप्त करें

ऐरे के अंदर ऑब्जेक्ट के किसी भी फ़ील्ड से मैच करने के लिए $or उपयोग करें और limit(1) से केवल पहला परिणाम लें।

कलेक्शन बनाएं और डॉक्यूमेंट इन्सर्ट करें

> db.matchElementInArrayDemo.insertOne({
  "StudentName": "Chris",
  "StudentOtherDetails": [
    {"StudentCountryName": "US", "StudentSkills": "MongoDB"},
    {"StudentCountryName": "UK", "StudentSkills": "Java"}
  ]
});
{ "acknowledged": true, "insertedId": ObjectId("5cd423282cba06f46efe9ee2") }

> db.matchElementInArrayDemo.insertOne({
  "StudentName": "Chris",
  "StudentOtherDetails": [
    {"StudentCountryName": "AUS", "StudentSkills": "PHP"},
    {"StudentCountryName": "US", "StudentSkills": "MongoDB"}
  ]
});
{ "acknowledged": true, "insertedId": ObjectId("5cd423412cba06f46efe9ee3") }

सभी डॉक्यूमेंट देखें

> db.matchElementInArrayDemo.find().pretty();
{
  "_id": ObjectId("5cd423282cba06f46efe9ee2"),
  "StudentName": "Chris",
  "StudentOtherDetails": [
    {"StudentCountryName": "US", "StudentSkills": "MongoDB"},
    {"StudentCountryName": "UK", "StudentSkills": "Java"}
  ]
}
{
  "_id": ObjectId("5cd423412cba06f46efe9ee3"),
  "StudentName": "Chris",
  "StudentOtherDetails": [
    {"StudentCountryName": "AUS", "StudentSkills": "PHP"},
    {"StudentCountryName": "US", "StudentSkills": "MongoDB"}
  ]
}

$or से US देश या MongoDB स्किल वाले ऐरे एलिमेंट मैच करें

> db.matchElementInArrayDemo.find({
  $or: [
    {"StudentOtherDetails.StudentCountryName": "US"},
    {"StudentOtherDetails.StudentSkills": "MongoDB"}
  ]
}).limit(1);
{ "_id": ObjectId("5cd423282cba06f46efe9ee2"), "StudentName": "Chris", "StudentOtherDetails": [{"StudentCountryName": "US", "StudentSkills": "MongoDB"}, {"StudentCountryName": "UK", "StudentSkills": "Java"}] }

व्याख्या

  • StudentOtherDetails.StudentCountryName - ऐरे के हर ऑब्जेक्ट में चेक करता है
  • $or - दोनों कंडीशन में से कोई भी सच हो तो डॉक्यूमेंट मैच
  • limit(1) - केवल पहला मैचिंग डॉक्यूमेंट लौटाता है
  • दोनों डॉक्यूमेंट मैच करते हैं (दोनों में US और MongoDB हैं), पहला लौटा

नोट: $elemMatch से अंतर

$or में प्रत्येक कंडीशन अलग-अलग ऐरे एलिमेंट पर लागू हो सकती है। अगर एक ही ऐरे एलिमेंट पर दोनों कंडीशन चाहिए तो $elemMatch उपयोग करें:

db.matchElementInArrayDemo.find({
  StudentOtherDetails: {
    $elemMatch: {
      $or: [
        {StudentCountryName: "US"},
        {StudentSkills: "MongoDB"}
      ]
    }
  }
})
  1. - MongoDB में सरणी मिलान कैसे लागू करें?

    सरणी मिलान के लिए $all का उपयोग करें। $all ऑपरेटर उन दस्तावेज़ों को चुनता है जहां किसी फ़ील्ड का मान एक सरणी है जिसमें सभी निर्दिष्ट तत्व होते हैं। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं: > db.demo668.createIndex({ListOfSubject:1}); { createdCollectionAutomatically : true, numIndexesBefore : 1, n

  1. - MongoDB Array में Element Remove करना

    Element remove करने के लिए update में $pull का उपयोग करें। $pull operator existing array से उन सभी instances को remove करता है जो specified condition से match करते हैं। Collection बनाना db.demo541.insertOne({software:{services:[gmail,facebook,yahoo]}}); { acknowledged : true, insertedId : ObjectId(

  1. - MongoDB में JSON सरणी के आंतरिक तत्व तक पहुंचें

    MongoDB में JSON सरणी के आंतरिक तत्व तक पहुंचने के लिए डॉट नोटेशन का उपयोग करें। आइए एक कलेक्शन बनाते हैं: > db.demo687.insert({CountryName:US, info: { id:101, details: [ { Name:Chris, SubjectName:MongoDB, otherDetails:{ Marks:58, Age:23 } } ] } } ) WriteResult({ nInserted : 1 }) > db.demo687