Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs 菜单
Docs 主页
/ /

查询 Null 字段或缺失字段

使用以下方法在MongoDB中查询 null 或缺失的字段:

  • 您的编程语言的驱动程序。

  • MongoDB Atlas 用户界面。请参阅使用 MongoDB Atlas 查询 null 或缺失字段,了解详情。

  • MongoDB Compass。


➤ 使用右上角的 Select your language(选择语言)下拉菜单,设置以下示例的语言或选择 MongoDB Compass。


MongoDB中的查询运算符以不同方式处理 null 值。

本页提供的操作示例使用 mongosh 中的 db.collection.find()方法查询null

本页提供了使用 MongoDB Compass 查询 null 值的操作示例。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用mongoc_collection_find_with_opts查询null 值的操作示例。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用MongoDB nullC#驱动程序中的 MongoCollection.Find() 方法查询 值的操作示例。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用MongoDB nullGo驱动程序中的 Collection.Find 函数查询 值的操作示例。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用null com.mongodb.reactivestreams查询 值的操作示例。 MongoDB Java Reactive Streams 驱动程序中的客户端方法。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用null com.mongodb查询 值的操作示例。 MongoDB Java同步驱动程序中的客户端方法。

提示

此驱动程序提供了 com.mongodb.client.model.Filters 辅助方法,以便于创建筛选器文档。此页面中的示例使用这些方法创建筛选器文档。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用MongoCollection.find()查询null值的操作示例。MongoDB Kotlin协程驱动程序中的方法。

提示

此驱动程序提供了 com.mongodb.client.model.Filters 辅助方法,以便于创建筛选器文档。此页面中的示例使用这些方法创建筛选器文档。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页面提供的示例展示了使用 motor.motor_asyncio.AsyncIOMotorCollection.find 方法在 Motor 驱动程序中查询 null 值的操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的操作示例使用Collection.find()查询nullMongoDB Node.js驱动程序中的方法。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的操作示例使用null MongoDB PHP库MongoDB\\Collection::find() 中的 方法查询 值。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用 pymongo.collection.Collection.find 方法在 PyMongo Python 驱动程序中查询 null 值的操作示例。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的操作示例使用null MongoDB Ruby驱动程序 中的 Mongo::Collection#find() 方法查询 值。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页中的示例展示了使用 Collection.find() 方法查询 null 值的操作,方法在 MongoDB Scala 驱动程序中执行查询操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

重要

null 与MongoDB Shell结合使用来查询MongoDB中的 null 或缺失字段。

重要

null 与MongoDB Compass结合使用来查询MongoDB中的 null 或缺失字段。

重要

BCON_NULL 与 MongoDB C 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

BsonNull.Value 与 MongoDB C# 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

nil 与 MongoDB Go 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

null 与 Java Reactive Streams 驱动程序结合使用来查询 MongoDB 中的 null 或缺失字段。

重要

null 与 Java 驱动程序结合使用来查询 MongoDB 中的 null 或缺失字段。

重要

null 与 Kotlin 协程驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

None 与 Motor 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

null 与 Node.js 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

null 与 MongoDB PHP 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

None 与 PyMongo Python 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

nil 与 MongoDB Ruby 驱动程序结合使用,以查询 MongoDB 中的 null 或缺失字段。

重要

BsonNull() 与MongoDB Scala驱动程序结合使用,以查询MongoDB中的 null 或缺失字段。

本页上的示例使用 sample_mflix示例数据集中的数据。有关如何将此数据集加载到自管理MongoDB 部署中的详细信息,请参阅加载示例数据集。如果对示例数据库进行了任何修改,则可能需要删除并重新创建数据库才能运行本页上的示例。

[
{ "_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 := []any{
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()

{ metacritic : null }查询会将包含 metacritic字段与 null不包含 metacritic字段的文档进行匹配。

{ 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.movies.find( { metacritic: null } )

将以下查询过滤文档复制到查询栏中,然后单击 Find

{ item: null }
查询 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()))

该查询返回 movies集合中 metacritic字段包含 null 值或不存在的所有文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

该查询会返回集合中的这两份文档。

要查询存在不为空的字段,请使用 { $ne : null }过滤器。

{ metacritic : { $ne : null } }查询匹配存在metacritic 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

{ item : { $ne : null } }查询匹配存在item 字段且具有非空值的文档。

db.movies.find( { metacritic: { $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));

{ metacritic : { $type: 10 } } 查询 匹配包含具有 null 值的 metacritic 字段的文档。The value of the metacritic 字段 is of BSON Type Null (BSON Type 10):

{ item : { $type: 10 } } 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

{ item, { $type, BCON_NULL } } 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

Type("item", BsonType.Null)使用 FilterDefinitionBuilder.Type() 方法的 查询仅匹配包含item 字段和null 值的文档。item字段的值为BSON类型 Null(BSON类型 10):

以下查询仅匹配 包含item 字段且值为BSON Type Null (BSON类型10 )的文档:

type("item", BsonType.NULL) 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

type("item", BsonType.NULL) 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

type("item", BsonType.NULL) 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

{ item : { $type: 10 } } 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

{ item : { $type: 10 } } 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

[ item => [ $type => 10 ] ] 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

{ item : { $type: 10 } } 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

{ item => { $type => 10 } } 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

type("item", BsonType.NULL) 查询 匹配包含具有 null 值的 item 字段的文档。The value of the item 字段 is of BSON Type Null (BSON Type 10):

db.movies.find( { metacritic: { $type: 10 } } )

将以下查询过滤器文档复制到查询栏,然后单击 Find

{ item : { $type: 10 } }
查找 null 类型
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))

该查询仅返回 movies集合中 metacritic字段具有 null 值的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

该查询仅返回 item 字段值为 null 的文档。

文档的以下示例查询不包含字段。

{ metacritic : { $exists: false } } 查询匹配不包含 metacritic 字段的文档:

{ item : { $exists: false } } 查询匹配不包含 item 字段的文档:

{ item, { $exists, BCON_BOOL (false) } } 查询匹配不包含 item 字段的文档:

使用 FilterDefinitionBuilder.Exists() 方法的 Exists("item", false) 查询匹配不包含 item 字段的文档:

bson.D{{"item", bson.D{{"$exists", false}}}} 查询匹配不包含 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.movies.find( { metacritic: { $exists: false } } )

将以下查询过滤器文档复制到查询栏,然后单击 Find

{ item : { $exists: false } }
查询 null 值
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))

该查询仅返回 movies集合中不包含 metacritic字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

该查询仅返回包含 item 字段的文档。

注意

表达式不支持$exists操作符。要检查表达式中某个字段是否存在,可以使用$type聚合操作符来检查字段的类型是否为 missing

有关更多信息,请参阅 $type 存在性检查。

提示

$type$exists 操作符的参考文档。

本节中的示例使用示例训练数据集。要了解如何将示例数据集加载到您的 MongoDB Atlas 部署中,请参阅加载示例数据。

要查询 MongoDB Atlas 中的 null 或缺失字段,请遵循以下步骤:

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。

  3. 在侧边栏中,单击 Database 标题下的 Clusters

    会显示集群页面。

2
  1. 对于包含样本数据的集群,单击Browse Collections

  2. 在左侧导航窗格中,选择 sample_training 数据库。

  3. 选择 companies 集合。

3

单击Insert Document按钮显示对话框,然后单击Insert插入仅包含 _id 字段的文档。

4

要查找包含 null 或缺失值的文档,请在Filter字段中指定查询筛选器文档。查询筛选器文档使用查询运算符指定搜索条件。

MongoDB中的查询运算符以不同方式处理 null 值。要应用查询筛选条件,请将以下每个文档复制到 Filter搜索栏中,然后单击 Apply

使用以下查询筛选条件匹配包含 description字段和 null 值或不包含 description字段的文档:

{ description : null }

使用以下查询筛选器仅匹配包含 description 字段和 null 值的文档。筛选器指定该字段的值必须是 BSON 类型 Null(BSON 类型 10):

{ description : { $type: 10 } }

使用以下查询筛选器仅匹配不包含 description 字段的文档。只有您之前插入的文档应该会显示:

{ description : { $exists: false } }

后退

项目成果

在此页面上