查询空或缺失字段
您可以查询使用以下方法在MongoDB中检测null
或缺失的字段
您编程语言的驱动程序。
以下MongoDB Atlas UI。要了解更多信息,请参阅使用MongoDB Atlas查询null或缺失字段。
➤使用右上角的选择语言下拉菜单设置以下示例的语言或选择MongoDB Compass。
MongoDB中的不同查询运算符对null
值的处理方式不同。
此页提供了查询null
值的操作示例使用db.collection.find()
方法在mongosh
.
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用MongoDB Compass.
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用mongoc_collection_find_with_opts。.. include:: /includes/driver-examples/examples-intro.rst
此页提供了查询null
值的操作示例使用MongoCollection.Find()方法在MongoDB C# Driver中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用Collection.Find函数在MongoDB Go Driver中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用MongoDB的com.mongodb.reactivestreams.client.MongoCollection.find方法,该方法是MongoDB Java Reactive Streams Driver的一部分。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用MongoDB的com.mongodb.client.MongoCollection.find方法,在Java 同步驱动程序。
提示
该驱动程序提供了com.mongodb.client.model.Filters辅助方法,以方便创建过滤文档。本页上的示例使用这些方法创建过滤文档。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例通过使用MongoDB的MongoCollection.find()方法,在Kotlin Coroutine Driver.中。
提示
该驱动程序提供了com.mongodb.client.model.Filters辅助方法,以简化创建过滤器文档的过程。本页的示例使用这些方法创建过滤器文档。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用motor.motor_asyncio.AsyncIOMotorCollection.find
方法在Motor驱动程序中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用 Collection.find() 方法在 MongoDB Node.js 驱动程序。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用 MongoDB\\Collection::find()
方法在 MongoDB PHP 库 中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用pymongo.collection.Collection.find
方法在PyMongo Python驱动程序中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用Mongo::Collection#find()方法在MongoDB Ruby Driver.中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
此页提供了查询null
值的操作示例使用 collection.find() 方法在 MongoDB Scala Driver 中。
此页上的示例使用inventory
集合。连接到MongoDB实例中的测试数据库,然后使用以下命令创建inventory
集合
重要
在 MongoDB C 驱动程序中使用 BCON_NULL
来查询 MongoDB 中的 null
或缺失字段。
重要
在 MongoDB C# 驱动程序中使用 BsonNull.Value
来查询 MongoDB 中的 null
或缺失字段。
重要
在 MongoDB Go 驱动程序中使用 nil
来查询 MongoDB 中的 null
或缺失字段。
重要
在 Kotlin Coroutine 驱动程序中使用 null
来查询 MongoDB 中的 null
或缺失字段。
重要
在 Motor 驱动程序中使用 None
来查询 MongoDB 中的 null
或缺失字段。
重要
在 PyMongo Python 驱动程序中使用 None
来查询 MongoDB 中的 null
或缺失字段。
重要
在 MongoDB Ruby 驱动程序中使用 nil
来查询 MongoDB 中的 null
或缺失字段。
重要
在 MongoDB Scala 驱动程序中使用 BsonNull()
来查询 MongoDB 中的 null
或缺失字段。
db.inventory.insertMany([ { _id: 1, item: null }, { _id: 2 } ])
[ { "_id": 1, "item": null }, { "_id": 2 } ]
有关在 MongoDB Compass 中插入文档的说明,请参阅 插入文档。
mongoc_collection_t *collection; mongoc_bulk_operation_t *bulk; bson_t *doc; bool r; bson_error_t error; bson_t reply; collection = mongoc_database_get_collection (db, "inventory"); bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL); doc = BCON_NEW ( "_id", BCON_INT64 (1), "item", BCON_NULL); r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error); bson_destroy (doc); if (!r) { MONGOC_ERROR ("%s\n", error.message); goto done; } doc = BCON_NEW ("_id", BCON_INT64 (2)); r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error); bson_destroy (doc); if (!r) { MONGOC_ERROR ("%s\n", error.message); goto done; } /* "reply" is initialized on success or error */ r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error); if (!r) { MONGOC_ERROR ("%s\n", error.message); }
var documents = new[] { new BsonDocument { { "_id", 1 }, { "item", BsonNull.Value } }, new BsonDocument { { "_id", 2 } } }; collection.InsertMany(documents);
docs := []interface{}{ bson.D{ {"_id", 1}, {"item", nil}, }, bson.D{ {"_id", 2}, }, } result, err := coll.InsertMany(context.TODO(), docs)
Publisher<Success> insertManyPublisher = collection.insertMany(asList( Document.parse("{'_id': 1, 'item': null}"), Document.parse("{'_id': 2}") ));
collection.insertMany(asList( Document.parse("{'_id': 1, 'item': null}"), Document.parse("{'_id': 2}") ));
collection.insertMany( listOf( Document("_id", 1) .append("item", null), Document("_id", 2) ) )
await db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);
$insertManyResult = $db->inventory->insertMany([ ['_id' => 1, 'item' => null], ['_id' => 2], ]);
db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
client[:inventory].insert_many([{ _id: 1, item: nil }, { _id: 2 }])
collection.insertMany(Seq( Document("""{"_id": 1, "item": null}"""), Document("""{"_id": 2}""") )).execute()
等值过滤器
查询 { item : null }
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 { item : null }
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 { item, BCON_NULL }
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
使用 FilterDefinitionBuilder.Eq() 方法进行查询 Eq("item", BsonNull.Value)
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 item => nil
匹配包含 item
字段且值为 nil
的文档,或者不包含 item
字段的文档。
查询 eq("item", null)
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 eq("item", null)
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 eq("item", null)
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 { item : None }
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 { item : null }
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 [ item => undef ]
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
查询 { item : None }
匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
{ item => nil }
查询匹配包含 item
字段且值为 nil
的文档,或者不包含 item
字段的文档。
equal("item", BsonNull)
查询匹配包含 item
字段且值为 null
的文档,或者不包含 item
字段的文档。
db.inventory.find( { item: null } )
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ("item", BCON_NULL); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("item", BsonNull.Value); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", nil}, })
FindPublisher<Document> findPublisher = collection.find(eq("item", null));
FindIterable<Document> findIterable = collection.find(eq("item", null));
val findFlow = collection .find(eq("item", null))
cursor = db.inventory.find({"item": None})
const cursor = db.collection('inventory').find({ item: null });
$cursor = $db->inventory->find(['item' => null]);
cursor = db.inventory.find({"item": None})
client[:inventory].find(item: nil)
var findObservable = collection.find(equal("item", BsonNull()))
查询返回集合中的所有文档。
非相等过滤器
要查询存在且非空的字段,请使用 { $ne : null }
过滤器。查询 { item : { $ne : null } }
匹配 item
字段存在且具有非空值的文档。
db.inventory.find( { item: { $ne : null } } )
{ item: { $ne : null } }
filter = BCON_NEW ("item", BCON_NULL); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Ne("item", BsonNull.Value); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", bson.D{"$ne": nil}}, })
db.inventory.find( { item: { $ne : null} } )
collection.find(ne("item", null));
collection.find(ne("item", null))
cursor = db.inventory.find( { "item": { "$ne": None } } )
const cursor = db.collection('inventory') .find({ item: { $ne : null } });
$cursor = $db->inventory->find(['item' => ['$ne' => null ]]);
cursor = db.inventory.find( { "item": { "$ne": None } } )
client[:inventory].find(item: { '$ne' => nil })
collection.find($ne("item", null));
类型检查
{ item : { $type: 10 } }
查询匹配 仅 包含 item
字段且值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
{ item : { $type: 10 } }
查询匹配 仅 包含 item
字段且值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
{ item, { $type, BCON_NULL } }
查询匹配 仅 包含 item
字段且值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
The Type("item", BsonType.Null)
查询使用 FilterDefinitionBuilder.Type() 方法仅匹配包含 item
字段且其值为 null
的文档。item
字段的值是 BSON 类型 Null
(BSON 类型 10)
以下查询仅匹配包含 item
字段且其值为 BSON 类型 Null
(BSON 类型 10) 的文档。
type("item", BsonType.NULL)
查询仅匹配包含 item
字段且其值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
type("item", BsonType.NULL)
查询仅匹配包含 item
字段且其值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
type("item", BsonType.NULL)
查询仅匹配包含 item
字段且其值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
type("item", BsonType.NULL)
查询仅匹配包含 item
字段且其值为 null
的文档。这意味着 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
{ item : { $type: 10 } }
查询匹配 仅 包含 item
字段且值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
{ item : { $type: 10 } }
查询匹配 仅 包含 item
字段且值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
[ item => [ $type => 10 ] ]
查询仅匹配包含 item
字段且其值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
{ item : { $type: 10 } }
查询匹配 仅 包含 item
字段且值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
{ item => { $type => 10 } }
查询仅匹配包含 item
字段且其值为 null
的文档;即 item
字段的值是 BSON 类型 Null
(BSON 类型 10)
db.inventory.find( { item : { $type: 10 } } )
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "item", "{", "$type", BCON_INT64 (10), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Type("item", BsonType.Null); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", bson.D{ {"$type", 10}, }}, })
findPublisher = collection.find(type("item", BsonType.NULL));
findIterable = collection.find(type("item", BsonType.NULL));
val findFlow = collection .find(type("item", BsonType.NULL))
cursor = db.inventory.find({"item": {"$type": 10}})
const cursor = db.collection('inventory').find({ item: { $type: 10 } });
$cursor = $db->inventory->find(['item' => ['$type' => 10]]);
cursor = db.inventory.find({"item": {"$type": 10}})
client[:inventory].find(item: { '$type' => 10 })
findObservable = collection.find(bsonType("item", BsonType.NULL))
查询仅返回 item
字段值为 null
的文档。
存在性检查
以下示例查询不包含字段的文档。[1]
查询 { item : { $exists: false } }
匹配不包含 item
字段的文档
查询 { item : { $exists: false } }
匹配不包含 item
字段的文档
查询 { item, { $exists, BCON_BOOL (false) } }
匹配不包含 item
字段的文档
使用 FilterDefinitionBuilder.Exists() 方法匹配不包含 item
字段的文档
查询 exists("item", false)
匹配不包含 item
字段的文档
查询 exists("item", false)
匹配不包含 item
字段的文档
查询 exists("item", false)
匹配不包含 item
字段的文档
查询 { item : { $exists: False } }
匹配不包含 item
字段的文档
查询 { item : { $exists: false } }
匹配不包含 item
字段的文档
查询 [ item => [ $exists => false ] ]
匹配不包含 item
字段的文档
查询 { item : { $exists: False } }
匹配不包含 item
字段的文档
查询 { item => { $exists => false } }
匹配不包含 item
字段的文档
查询 exists("item", exists = false)
匹配不包含 item
字段的文档
db.inventory.find( { item : { $exists: false } } )
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "item", "{", "$exists", BCON_BOOL (false), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
请确保通过调用以下方法清理所有打开的资源,根据需要
var filter = Builders<BsonDocument>.Filter.Exists("item", false); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"item", bson.D{ {"$exists", false}, }}, })
findPublisher = collection.find(exists("item", false));
findIterable = collection.find(exists("item", false));
val findFlow = collection .find(exists("item", false))
cursor = db.inventory.find({"item": {"$exists": False}})
const cursor = db.collection('inventory').find({ item: { $exists: false } });
$cursor = $db->inventory->find(['item' => ['$exists' => false]]);
cursor = db.inventory.find({"item": {"$exists": False}})
client[:inventory].find(item: { '$exists' => false })
findObservable = collection.find(exists("item", exists = false))
查询仅返回不包含 item 字段的文档。
[1] | 用户不能再使用查询过滤器 $type: 0 作为 $exists:false 的同义词。要查询空或缺失的字段,请参阅查询空或缺失字段。 |
使用 MongoDB Atlas 查询空或缺失字段
本节中的示例使用示例训练数据集。要了解如何将示例数据集加载到您的 MongoDB Atlas 部署中,请参阅加载数据。
要在 MongoDB Atlas 中查询空或缺失字段,请按照以下步骤操作
指定一个查询过滤器文档。
要查找包含null
或缺失值的文档,在过滤器字段中指定查询过滤器文档。查询过滤器文档使用查询运算符来指定搜索条件。
MongoDB 中的不同查询运算符对 null
值的处理方式不同。要应用查询过滤器,请将以下每个文档复制到 过滤器 搜索栏中,然后点击 应用。
使用以下查询过滤器来匹配包含具有 null
值的 description
字段或不含 description
字段的文档。
{ description : null }
使用以下查询过滤器来匹配仅包含具有 null
值的 description
字段的文档。此过滤器指定字段值必须为 BSON 类型 Null
(BSON 类型 10)。
{ description : { $type: 10 } }
使用以下查询过滤器来匹配仅不含 description
字段的文档。只有您之前插入的文档应该显示出来。
{ description : { $exists: false } }