Select MongoDB documents where a field either does not exist, is null, or is false?


You can use $in operator for this. Let us first create a collection with a document. The query to create a collection with a document is as follows −

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

Display all documents from a collection with the help of find() method. The query is as follows −

> db.selectMongoDBDocumentsWithSomeCondition.find().pretty();

The following is the output &minus

{
   "_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
}

Here is the query to select MongoDB documents where a field either does not exist, is null, or is false −

> db.selectMongoDBDocumentsWithSomeCondition.find({ "hasAgeGreaterThanOrEqualTo18": {$in: [false,null]}}).pretty();

The following is the output −

{
   "_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
}

Updated on: 30-Jul-2019

320 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements