การค้นหาเอกสารที่ค่าฟิลด์ลงท้ายด้วยอักขระเฉพาะใน MongoDB
ใช้ $regex operator ร่วมกับ Regular Expression anchor $ (end of string) เพื่อค้นหาเอกสารที่ค่าของฟิลด์ลงท้ายด้วยอักขระหรือข้อความที่กำหนด
ไวยากรณ์
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" }
ค้นหาเอกสารที่ StudentCountryName ลงท้ายด้วย 'S'
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find({StudentCountryName: {$regex: "S$"}}).pretty();
ผลลัพธ์ (LAOS, AUS, US):
{ "_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" }
คำอธิบาย Regular Expression
- $ : Anchor หมายถึง "จบสตริง" (end of string)
- S$ : ตรงกับสตริงที่ลงท้ายด้วยตัวอักษร 'S'
- ^pattern$ : ตรงกับสตริงทั้งหมด (เริ่มต้นด้วย pattern และจบด้วย pattern)
ตัวอย่างเพิ่มเติม
// ลงท้ายด้วย "land"
db.collection.find({ field: { $regex: "land$" } })
// ลงท้ายด้วยตัวเลข
db.collection.find({ field: { $regex: "[0-9]$" } })
// ไม่สนใจตัวพิมพ์เล็ก/ใหญ่ (case insensitive)
db.collection.find({ field: { $regex: "S$", $options: "i" } })
ข้อควรระวังด้านประสิทธิภาพ
- Regex ที่ขึ้นต้นด้วย ^ (เช่น ^pattern) สามารถใช้ Index ได้
- Regex ที่ลงท้ายด้วย $ (เช่น pattern$) ไม่สามารถใช้ Index ได้ ต้องทำ Collection Scan
- สำหรับคอลเลกชันขนาดใหญ่ ควรพิจารณาใช้ Text Index หรือเก็บค่าที่ประมวลผลไว้ล่วงหน้า