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

MongoDB: ทางเลือกแทน $eq สำหรับการจับคู่ค่าในอาร์เรย์


สร้างคอลเลกชันตัวอย่าง

> db.demo145.insertOne({"ListOfNames":["Chris","David","Mike"]});
{ "acknowledged": true, "insertedId": ObjectId("5e32f37bfdf09dd6d08539bb") }
> db.demo145.insertOne({"ListOfNames":["Bob","John"]});
{ "acknowledged": true, "insertedId": ObjectId("5e32f384fdf09dd6d08539bc") }

แสดงข้อมูล

> db.demo145.find();
{ "_id": ObjectId("5e32f37bfdf09dd6d08539bb"), "ListOfNames": [ "Chris", "David", "Mike" ] }
{ "_id": ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames": [ "Bob", "John" ] }

คิวรี: หาเอกสารที่มี "John" ใน ListOfNames (ไม่ต้องเขียน $eq)

> db.demo145.find({"ListOfNames":"John"});

ผลลัพธ์: ได้เอกสารที่สอง

{ "_id": ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames": [ "Bob", "John" ] }