MongoDB में विशेष कैरेक्टर से समाप्त होने वाले मानों वाले दस्तावेज़ प्राप्त करना
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") }
find() मेथड से सभी दस्तावेज़ प्रदर्शित करना
> 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" }
"S" कैरेक्टर से समाप्त होने वाले मानों वाले दस्तावेज़ प्राप्त करने का क्वेरी
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find({StudentCountryName: {$regex: "S$"}}).pretty();
यह निम्नलिखित आउटपुट देगा (LAOS, AUS, US - सभी "S" से समाप्त होते हैं):
{ "_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" }