คุณสามารถใช้ตัวดำเนินการ $in สำหรับสิ่งนี้ ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน แบบสอบถามเพื่อสร้างคอลเลกชันที่มีเอกสารมีดังนี้ -
> db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":1,"StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010215705caea966c557f") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":2,"StudentName":"Mike","hasAgeGreaterThanOrEqualTo18":true}); { "acknowledged" : true, "insertedId" : ObjectId("5c90106a5705caea966c5580") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":3,"StudentName":"Carol","hasAgeGreaterThanOrEqualTo18":false}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010795705caea966c5581") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":4,"StudentName":"Sam","hasAgeGreaterThanOrEqualTo18":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010865705caea966c5582") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":5,"StudentName":"David","hasAgeGreaterThanOrEqualTo18":false}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010945705caea966c5583") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":6,"StudentName":"Chris","hasAgeGreaterThanOrEqualTo18":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010a45705caea966c5584") } > db.selectMongoDBDocumentsWithSomeCondition.insertOne({"StudentId":7,"StudentName":"Robert","hasAgeGreaterThanOrEqualTo18":true}); { "acknowledged" : true, "insertedId" : ObjectId("5c9010b05705caea966c5585") }
แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้เมธอด find() แบบสอบถามมีดังต่อไปนี้ −
> db.selectMongoDBDocumentsWithSomeCondition.find().pretty();
ต่อไปนี้เป็นผลลัพธ์ &ลบ
{ "_id" : ObjectId("5c9010215705caea966c557f"), "StudentId" : 1, "StudentName" : "Larry" } { "_id" : ObjectId("5c90106a5705caea966c5580"), "StudentId" : 2, "StudentName" : "Mike", "hasAgeGreaterThanOrEqualTo18" : true } { "_id" : ObjectId("5c9010795705caea966c5581"), "StudentId" : 3, "StudentName" : "Carol", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010865705caea966c5582"), "StudentId" : 4, "StudentName" : "Sam", "hasAgeGreaterThanOrEqualTo18" : null } { "_id" : ObjectId("5c9010945705caea966c5583"), "StudentId" : 5, "StudentName" : "David", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010a45705caea966c5584"), "StudentId" : 6, "StudentName" : "Chris", "hasAgeGreaterThanOrEqualTo18" : null } { "_id" : ObjectId("5c9010b05705caea966c5585"), "StudentId" : 7, "StudentName" : "Robert", "hasAgeGreaterThanOrEqualTo18" : true }
นี่คือแบบสอบถามเพื่อเลือกเอกสาร MongoDB ที่ไม่มีฟิลด์ใด ๆ เป็นโมฆะหรือเป็นเท็จ -
> db.selectMongoDBDocumentsWithSomeCondition.find({ "hasAgeGreaterThanOrEqualTo18": {$in: [false,null]}}).pretty();
ต่อไปนี้เป็นผลลัพธ์ -
{ "_id" : ObjectId("5c9010215705caea966c557f"), "StudentId" : 1, "StudentName" : "Larry" } { "_id" : ObjectId("5c9010795705caea966c5581"), "StudentId" : 3, "StudentName" : "Carol", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010865705caea966c5582"), "StudentId" : 4, "StudentName" : "Sam", "hasAgeGreaterThanOrEqualTo18" : null } { "_id" : ObjectId("5c9010945705caea966c5583"), "StudentId" : 5, "StudentName" : "David", "hasAgeGreaterThanOrEqualTo18" : false } { "_id" : ObjectId("5c9010a45705caea966c5584"), "StudentId" : 6, "StudentName" : "Chris", "hasAgeGreaterThanOrEqualTo18" : null }