使用 $exists 判断字段是否存在
> db.book.find({})
{
"_id" : ObjectId("58b395fbab449b190054c556"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"owner" : "Knight"
}
{
"_id" : ObjectId("58b3960dab449b190054c557"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"owner" : "Knight",
"date" : "2017-02-27"
}
> db.book.find({date:{$exists:true}})
{
"_id" : ObjectId("58b3960dab449b190054c557"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"owner" : "Knight",
"date" : "2017-02-27"
}
> db.book.find({date:{$exists:false}})
{
"_id" : ObjectId("58b395fbab449b190054c556"),
"title" : "MongoDB 教程",
"description" : "MongoDB 是一个 Nosql 数据库",
"owner" : "Knight"
}