Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

วิธีค้นหาเอกสารด้วย _id ใน MongoDB

การค้นหาเอกสารด้วย _id ใน MongoDB

ใช้ find() ร่วมกับ ObjectId() เพื่อค้นหาเอกสารเฉพาะรายการด้วย _id ได้อย่างแม่นยำ

ไวยากรณ์

db.yourCollectionName.find({"_id": yourObjectId});

ตัวอย่างการสร้างคอลเลกชันและเพิ่มข้อมูล

> db.findRecordByIdDemo.insertOne({"CustomerName":"Larry","CustomerAge":26});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9dc2c875e2eeda1d5c3671") }
> db.findRecordByIdDemo.insertOne({"CustomerName":"Bob","CustomerAge":20});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9dc2d175e2eeda1d5c3672") }
> db.findRecordByIdDemo.insertOne({"CustomerName":"Carol","CustomerAge":22});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9dc2d775e2eeda1d5c3673") }
> db.findRecordByIdDemo.insertOne({"CustomerName":"David","CustomerAge":24});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9dc2e375e2eeda1d5c3674") }

แสดงข้อมูลทั้งหมด

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

ผลลัพธ์:

{ "_id" : ObjectId("5c9dc2c875e2eeda1d5c3671"), "CustomerName" : "Larry", "CustomerAge" : 26 }
{ "_id" : ObjectId("5c9dc2d175e2eeda1d5c3672"), "CustomerName" : "Bob", "CustomerAge" : 20 }
{ "_id" : ObjectId("5c9dc2d775e2eeda1d5c3673"), "CustomerName" : "Carol", "CustomerAge" : 22 }
{ "_id" : ObjectId("5c9dc2e375e2eeda1d5c3674"), "CustomerName" : "David", "CustomerAge" : 24 }

ค้นหาเอกสารด้วย _id เฉพาะ (Bob)

> db.findRecordByIdDemo.find({"_id": ObjectId("5c9dc2d175e2eeda1d5c3672")}).pretty();

ผลลัพธ์:

{ "_id" : ObjectId("5c9dc2d175e2eeda1d5c3672"), "CustomerName" : "Bob", "CustomerAge" : 20 }

คำอธิบาย _id และ ObjectId

  • _id : Primary Key ของ MongoDB Unique ต่อ Collection
  • ObjectId : Default _id Type (12-byte: Timestamp 4 bytes + Machine 3 bytes + Process 2 bytes + Counter 3 bytes)
  • Custom _id : สามารถกำหนด _id เองได้ (String, Number, Object) ตอน Insert

วิธีค้นหา _id แบบต่างๆ

1. ใช้ ObjectId() Constructor (Standard)

db.collection.find({ _id: ObjectId("5c9dc2d175e2eeda1d5c3672") });

2. ใช้ findOne() (คืน Document โดยตรง ไม่ใช่ Cursor)

> var doc = db.findRecordByIdDemo.findOne({ _id: ObjectId("5c9dc2d175e2eeda1d5c3672") });
> doc;
{ "_id" : ObjectId("5c9dc2d175e2eeda1d5c3672"), "CustomerName" : "Bob", "CustomerAge" : 20 }

3. Custom _id (String/Number)

> db.customIdDemo.insertOne({ _id: "CUST-001", name: "John" });
> db.customIdDemo.find({ _id: "CUST-001" });

4. ใช้ Variable เก็บ ObjectId

> var myId = ObjectId("5c9dc2d175e2eeda1d5c3672");
> db.findRecordByIdDemo.find({ _id: myId });

Projection: ดึงเฉพาะฟิลด์ที่ต้องการ

> db.findRecordByIdDemo.find(
    { _id: ObjectId("5c9dc2d175e2eeda1d5c3672") }, 
    { CustomerName: 1, CustomerAge: 1, _id: 0 }
);

ผลลัพธ์:

{ "CustomerName" : "Bob", "CustomerAge" : 20 }

การใช้งานใน Application (Driver)

// Node.js MongoDB Driver
const { ObjectId } = require('mongodb');
const doc = await collection.findOne({ _id: new ObjectId("5c9dc2d175e2eeda1d5c3672") });

// Python PyMongo
from bson.objectid import ObjectId
doc = collection.find_one({ "_id": ObjectId("5c9dc2d175e2eeda1d5c3672") })

ข้อควรระวัง

  • ObjectId String: ต้องเป็น 24 Hex Characters เท่านั้น
  • Invalid ObjectId: จะ Error: "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"
  • Performance: _id มี Index อัตโนมัติ (Unique Index) Query เร็วมาก O(1)
  • Sharding: ถ้า Shard Key ไม่ใช่ _id อาจต้อง Query ทุก Shard

สรุป

  • ใช้ findOne() สำหรับดึง 1 เอกสาร (คืน Document โดยตรง)
  • ใช้ find() สำหรับ Cursor (เหมาะสำหรับ Iterate)
  • _id Index เป็น Unique และสร้างอัตโนมัติ
  • Custom _id ช่วยให้ Query ง่ายขึ้นในบางกรณี (เช่น User ID จากระบบภายนอก)