选择MongoDB文档,其中字段不存在,为null或为false?

您可以为此使用$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()method的帮助下显示集合中的所有文档。查询如下-

> 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
}