Computer >> Máy Tính >  >> Lập trình >> MongoDB

Làm cách nào để truy xuất các tài liệu có giá trị kết thúc bằng một ký tự cụ thể trong MongoDB?

Sau đây là cú pháp để truy xuất các tài liệu có giá trị kết thúc bằng một ký tự cụ thể trong MongoDB

db.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty();

Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu

>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")
}

Sau đây là truy vấn để hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find ()

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

Điều này sẽ tạo ra kết quả sau

{
   "_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"
}

Sau đây là truy vấn để truy xuất các tài liệu có giá trị kết thúc bằng một ký tự cụ thể trong MongoDB. Chúng tôi đang kiểm tra các giá trị kết thúc bằng ký tự “S”

> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find({StudentCountryName: {$regex: "S$"}}).pretty();

Điều này sẽ tạo ra kết quả sau

{
   "_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"
}