이를 위해 $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 }
다음은 필드가 존재하지 않거나 null이거나 false인 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 }