$exists is an element query operator that can be use to check the existense of any field in a document.
Syntax: { field: { $exists: } }
If { $exists: true }, it will matches the documents which contain the field.
If { $exists: false }, it will matches the documents which does not contain the field.
For example:
Employee Collection
In above collection, document {id : 2} does not have department field. So, lets say if we need to search the documents which does not have department field, the query would be:
Syntax: { field: { $exists:
If { $exists: true }, it will matches the documents which contain the field.
If { $exists: false }, it will matches the documents which does not contain the field.
For example:
Employee Collection
{
"_id" : 1,
"name" : {
"first" : "Abhi",
"last" : "Dev"
},
"department" : "finance",
"joineddate" : "2000-04-10"
},
{
"_id" : 2,
"name" : {
"first" : "Alok",
"last" : "Agrawal"
},
"joineddate" : "2006-07-07"
},
{
"_id" : 3,
"name" : {
"first" : "Dhruv",
"last" : "Sharma"
},
"department" : "finance",
"joineddate" : "2010-09-07"
},
{
"_id" : 4,
"name" : {
"first" : "Ravi",
"last" : "Mann"
},
"department" : "information",
"joineddate" : "2008-04-07"
}
}
In above collection, document {id : 2} does not have department field. So, lets say if we need to search the documents which does not have department field, the query would be:
db.employee.find({department: {$exists : false}})
Output of Above Query: {
"_id" : 2,
"name" : {
"first" : "Alok",
"last" : "Agrawal"
},
"joineddate" : "2006-07-07"
}
No comments:
Post a Comment