जिन दस्तावेज़ों का मान MongoDB में एक विशेष वर्ण के साथ समाप्त होता है, उन दस्तावेज़ों को पुनः प्राप्त करने के लिए सिंटैक्स निम्नलिखित है
db.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty(); आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं
>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Adam","StudentAge":25,"StudentCountryName":"LAOS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45b32d66697741252456")
}
>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Sam","StudentAge":24,"StudentCountryName":"ANGOLA"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45c02d66697741252457")
}
>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Robert","StudentAge":21,"StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45cb2d66697741252458")
}
>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Chris","StudentAge":20,"StudentCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45d92d66697741252459")
}
>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Larry","StudentAge":23,"StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45eb2d6669774125245a")
} खोज () विधि की सहायता से संग्रह से सभी दस्तावेज़ों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c9c45b32d66697741252456"),
"StudentName" : "Adam",
"StudentAge" : 25,
"StudentCountryName" : "LAOS"
}
{
"_id" : ObjectId("5c9c45c02d66697741252457"),
"StudentName" : "Sam",
"StudentAge" : 24,
"StudentCountryName" : "ANGOLA"
}
{
"_id" : ObjectId("5c9c45cb2d66697741252458"),
"StudentName" : "Robert",
"StudentAge" : 21,
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9c45d92d66697741252459"),
"StudentName" : "Chris",
"StudentAge" : 20,
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c9c45eb2d6669774125245a"),
"StudentName" : "Larry",
"StudentAge" : 23,
"StudentCountryName" : "US"
} निम्नलिखित दस्तावेजों को पुनः प्राप्त करने के लिए क्वेरी है जिनके मान MongoDB में किसी विशेष वर्ण के साथ समाप्त होते हैं। हम अक्षर "S" के साथ समाप्त होने वाले मानों की जांच कर रहे हैं
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find({StudentCountryName: {$regex: "S$"}}).pretty(); यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c9c45b32d66697741252456"),
"StudentName" : "Adam",
"StudentAge" : 25,
"StudentCountryName" : "LAOS"
}
{
"_id" : ObjectId("5c9c45cb2d66697741252458"),
"StudentName" : "Robert",
"StudentAge" : 21,
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9c45eb2d6669774125245a"),
"StudentName" : "Larry",
"StudentAge" : 23,
"StudentCountryName" : "US"
}