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

MongoDB में कई ऐरे एलिमेंट्स द्वारा फ़िल्टर कैसे करें

$elemMatch ऑपरेटर का उपयोग करके ऐरे में विशिष्ट एलिमेंट ढूंढें

$elemMatch ऑपरेटर उन डॉक्यूमेंट्स से मेल खाता है जिनमें एक ऐरे फ़ील्ड होता है जिसमें कम से कम एक एलिमेंट सभी निर्दिष्ट क्वेरी मापदंडों से मेल खाता है।

उदाहरण: संग्रह बनाना

> db.filterBySeveralElementsDemo.insertOne(
    {
        "_id": 100,
        "StudentDetails": [
            {"StudentName": "John", "StudentCountryName": "US"},
            {"StudentName": "Carol", "StudentCountryName": "UK"}
        ]
    }
);
{ "acknowledged": true, "insertedId": 100 }
> db.filterBySeveralElementsDemo.insertOne(
    {
        "_id": 101,
        "StudentDetails": [
            {"StudentName": "Sam", "StudentCountryName": "AUS"},
            {"StudentName": "Chris", "StudentCountryName": "US"}
        ]
    }
);
{ "acknowledged": true, "insertedId": 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"}
    ]
}

कई ऐरे एलिमेंट्स द्वारा फ़िल्टर करने की क्वेरी

StudentName: "Sam" और StudentCountryName: "AUS" दोनों वाले एलिमेंट वाले डॉक्यूमेंट्स ढूंढें:

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

आउटपुट:

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

केवल वही डॉक्यूमेंट वापस आया जिसमें ऐरे का एक एलिमेंट दोनों शर्तों को पूरा करता है।

  1. - MongoDB एग्रीगेशन: ऐरे के सबसेट एलिमेंट्स को जोड़ें

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

  1. - MongoDB में एम्बेडेड डॉक्यूमेंट की सरणी पर फ़िल्टर क्वेरी कैसे करें?

    इसके लिए MongoDB में aggregate() का उपयोग करें। आइए 3 स्तरों के नेस्टेड डॉक्यूमेंट्स के साथ एक कलेक्शन बनाते हैं: > db.demo736.insertOne( { _id: 101, details1: [ { details2: [ { details3: { Name: John } } ] } ] } ); { acknowledged : true, insertedId : 101 } > db.demo736.insertOne( { _id: 102,

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

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