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

MongoDB में कई सरणी तत्वों द्वारा फ़िल्टर करें?

<घंटा/>

इसके लिए आप $elemMatch ऑपरेटर का इस्तेमाल कर सकते हैं। $elemMatch ऑपरेटर उन दस्तावेज़ों से मेल खाता है जिनमें कम से कम एक तत्व के साथ एक सरणी फ़ील्ड होता है जो सभी निर्दिष्ट क्वेरी मानदंडों से मेल खाता है। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -

> db.filterBySeveralElementsDemo.insertOne( "_id":100, "StudentDetails":[ { "StudentName":"John", "StudentCountryName":"US", }, { "StudentName":"Carol", " StudentCountryName":"UK" } ]});{"स्वीकृत" :सच, "insertedId" :100 }> db.filterBySeveralElementsDemo.insertOne( { "_id":101, "StudentDetails":[ { "StudentName":"सैम" ", "StudentCountryName":"AUS", }, { "StudentName":"Chris", "StudentCountryName":"US" } ] });{ "स्वीकृत" :सच, "insertId" :101 }

खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -

> db.filterBySeveralElementsDemo.find().pretty();

यह निम्नलिखित आउटपुट देगा -

{ "_id" :100, "StudentDetails" :[ { "StudentName" :"John", "StudentCountryName" :"US" }, { "StudentName" :"Carol", "StudentCountryName" :"UK"} ]}{ "_id" :101, "StudentDetails" :[ { "StudentName" :"Sam", "StudentCountryName" :"AUS" }, { "StudentName" :"Chris", "StudentCountryName" :"US" } ] } 

कई सरणी तत्वों द्वारा फ़िल्टर करने की क्वेरी निम्नलिखित है -

> db.filterBySeveralElementsDemo.find({StudentDetails:{ $elemMatch:{StudentName:'Sam', StudentCountryName:'AUS' }}}).pretty();

यह निम्नलिखित आउटपुट देगा -

{ "_id" :101, "StudentDetails" :[ { "StudentName" :"Sam", "StudentCountryName" :"AUS" }, { "StudentName" :"Chris", "StudentCountryName" :"US" } ]} 
  1. MongoDB एकत्रीकरण में एक सरणी के तत्वों का सबसेट एक साथ कैसे जोड़ें?

    एक सरणी के तत्वों के सबसेट को एक साथ जोड़ने के लिए, $sum के साथ $first का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं - > db.demo610.insertOne({Values:[10,20,30,40,50]});{    "acknowledged" : true, "insertedId" : ObjectId("5e9747b8f57d0dc0b182d62e") }

  1. MongoDB के साथ एम्बेडेड दस्तावेज़ की सरणी पर फ़िल्टर क्वेरी?

    इसके लिए MongoDB में समुच्चय () का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं - > db.demo736.insertOne( ...    { ...       "_id": "101", ...       "details1": [ ...          { ...     &

  1. सी # प्रोग्राम एक विधेय के आधार पर सरणी तत्वों को फ़िल्टर करने के लिए

    एक सरणी सेट करें। int[] arr = { 40, 42, 12, 83, 75, 40, 95 }; 50 से ऊपर के तत्व प्राप्त करने के लिए व्हेयर क्लॉज और विधेय का उपयोग करें। IEnumerable<int> myQuery = arr.AsQueryable() .Where((a, index) => a >= 50); आइए देखें पूरा कोड - उदाहरण using System; using System.Linq; using System