选择既不存在、为空或为 false 的 MongoDB 中的字段中的记录?
你可以使用 $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 }
以下是如何选择字段不存在、为空或为 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 }
广告