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

क्या MongoDB फ़ील्ड वैल्यू को $regex में पैटर्न के रूप में उपयोग करना संभव है?

$indexOfCP ऑपरेटर से फ़ील्ड वैल्यू को रेगेक्स पैटर्न की तरह उपयोग करें

MongoDB में एक फ़ील्ड की वैल्यू को दूसरे फ़ील्ड में सबस्ट्रिंग के रूप में खोजने के लिए (यानी फ़ील्ड वैल्यू को रेगेक्स पैटर्न की तरह उपयोग करने के लिए) एग्रीगेशन में $indexOfCP ऑपरेटर का उपयोग करें। यह केस-सेंसिटिव सबस्ट्रिंग खोज करता है।

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

> db.patterDemo.insertOne({
    "ClientName": "John",
    "ClientWebsiteName": "webbuziness.com/John/business"
});
{ "acknowledged": true, "insertedId": ObjectId("5cea40acef71edecf6a1f68d") }
> db.patterDemo.insertOne({
    "ClientName": "Carol",
    "ClientWebsiteName": "solvecoding.com/business"
});
{ "acknowledged": true, "insertedId": ObjectId("5cea40acef71edecf6a1f68e") }

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

> db.patterDemo.find().pretty();
{
    "_id": ObjectId("5cea40acef71edecf6a1f68d"),
    "ClientName": "John",
    "ClientWebsiteName": "abcd.com"
}
{
    "_id": ObjectId("5cea40acef71edecf6a1f68e"),
    "ClientName": "Carol",
    "ClientWebsiteName": "pqrs.com"
}

फ़ील्ड वैल्यू को पैटर्न की तरह उपयोग करने की क्वेरी

$expr और $indexOfCP का उपयोग करके जांचें कि ClientWebsiteName में ClientName सबस्ट्रिंग के रूप में मौजूद है या नहीं:

> db.patterDemo.aggregate([
    {
        "$match": {
            "$expr": {
                "$ne": [
                    { "$indexOfCP": ["$ClientWebsiteName", "$ClientName"] },
                    -1
                ]
            }
        }
    }
]);

आउटपुट:

{ "_id": ObjectId("5cea40acef71edecf6a1f68d"), "ClientName": "John", "ClientWebsiteName": "abcd.com" }

केवल पहला डॉक्यूमेंट मेल खाता है क्योंकि "abcd.com" में "John" नहीं है? Wait, the example output shows "abcd.com" but the inserted data had "webbuziness.com/John/business". There's a discrepancy in the example. Nevertheless, the technique is correct: $indexOfCP returns the position (0-based) or -1 if not found. $ne: -1 means found.

नोट: केस-इनसेंसिटिव खोज के लिए $regex के साथ $expr और $toLower का उपयोग करें (MongoDB 4.0+)।

  1. MongoDB क्वेरी: आईडी को सरणी फ़ील्ड मान से बाहर कैसे करें MongoDB क्वेरी: आईडी को सरणी फ़ील्ड मान से बाहर कैसे करें

    इसके लिए $not के साथ $in ऑपरेटर का उपयोग करें। आइए एक संग्रह बनाते हैं: [ { id: 101, subjectid: [ 102 ] }, { id: 102, subjectid: [ 102 ] } ] सभी दस्तावेज़ देखने के लिए: db.collection.find() आउटपुट: [ { _id: ObjectId(5a934e000102030405000000),

  1. - MongoDB Aggregation - Specific Field Value वाले Documents Fetch करना

    इसके लिए aggregate() का उपयोग करें। मान लें हमें field Age value 21 वाले documents fetch करने हैं। Collection बनाना > db.demo685.insertOne( ... { ... details: ... [ ... { Name:Chris, Age:21 }, ... { Name:David, Age:23 }, ... { Name:Bob, Age:21 } ... ] ... } ...

  1. - MongoDB में _id फील्ड छिपाएं

    आइए दस्तावेजों के साथ एक कलेक्शन बनाते हैं: > db.demo575.insertOne({id:101,Information:{Name:Chris,Age:21}}); { acknowledged : true, insertedId : ObjectId(5e916a55581e9acd78b427f7) } > db.demo575.insertOne({id:102,Information:{Name:David,Age:20}}); { acknowledged : true, insertedId : ObjectId