如何根据字段不存在来查找 MongoDB 中的文档?
根据字段不存在来查找 MongoDB 中的文档,语法如下 −
db.yourCollectionName.find({ "yourFieldName" : { "$exists" : false } }).pretty();为了理解上面的语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下 −
> db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"John","StudentAge":25});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a5c629064dcd4a68b70e8")
}
> db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"David","StudentAge":26,"StudentMathMarks":78});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a5c809064dcd4a68b70e9")
}使用 find() 方法显示集合中的所有文档。查询如下 −
> db.findDocumentNonExistenceFieldDemo.find().pretty();
以下是输出 −
{
"_id" : ObjectId("5c8a5c629064dcd4a68b70e8"),
"StudentName" : "John",
"StudentAge" : 25
}
{
"_id" : ObjectId("5c8a5c809064dcd4a68b70e9"),
"StudentName" : "David",
"StudentAge" : 26,
"StudentMathMarks" : 78
}以下是如何根据字段不存在来查找文档的查询 −
> db.findDocumentNonExistenceFieldDemo.find({ "StudentMathMarks" : { "$exists" : false } }).pretty();以下是输出 −
{
"_id" : ObjectId("5c8a5c629064dcd4a68b70e8"),
"StudentName" : "John",
"StudentAge" : 25
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP