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

แบบสอบถาม MongoDB สำหรับการจับคู่แบบตรงทั้งหมด


สำหรับการจับคู่แบบตรงทั้งหมด คุณสามารถใช้ $exists ที่ตรวจสอบการจับคู่ ให้เราสร้างคอลเลกชันที่มีเอกสาร -

> db.demo290.insertOne({"ListOfName":"Chris"});{ "acknowledged" :true, "insertedId" :ObjectId("5e4c0c9e5d93261e4bc9ea2d")}> db.demo290.insertOne({"ListOfName" :["Chris","David"]});{ "acknowledged" :true, "insertedId" :ObjectId("5e4c0cb05d93261e4bc9ea2e")}

แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -

> db.demo290.find();

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

{ "_id" :ObjectId("5e4c0c9e5d93261e4bc9ea2d"), "ListOfName" :"Chris" }{ "_id" :ObjectId("5e4c0cb05d93261e4bc9ea2e"), "ListOfName" : 

นี่คือข้อความค้นหาสำหรับการจับคู่แบบตรงทั้งหมดของค่า -

> db.demo290.find({$and:[{'ListOfName.0':{$exists:false}}, {"ListOfName":'Chris'}]});

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

{ "_id" :ObjectId("5e4c0c9e5d93261e4bc9ea2d"), "ListOfName" :"Chris" }