Docs 菜单
Docs 主页
/
MongoDB Manual
/ /

查询 Null 字段或缺失字段

在此页面上

  • 相等筛选器
  • 非相等筛选器
  • 类型检查
  • 存在性检查
  • 使用 MongoDB Atlas 查询 Null 或缺失字段

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

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

  • MongoDB Atlas 用户界面。要了解更多信息,请参阅使用 MongoDB Atlas 查询空值或缺失字段

  • MongoDB Compass。


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


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

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

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

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

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

本页提供了使用null mongoc_collection_find_with_opts 查询 值的操作示例 。 .. include:: /includes/driver-examples/examples-intro.rst

本页面中的示例展示在 MongoDB C# 驱动程序中使用 MongoCollection.Find() 方法查询 null 值。

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

本页面中的示例展示在 MongoDB Go 驱动程序中使用 Collection.Find 函数执行查询 null 值的操作。

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

本页提供的操作示例使用null com.mongodb.reactivestreams.client.MongoCollection.find 查询 值 MongoDB Java Reactive Streams Driver 中的方法 。

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

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

提示

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

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

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

提示

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

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

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

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

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

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

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

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

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

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

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

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

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

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

重要

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

重要

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

重要

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

重要

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

重要

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

重要

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

重要

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

重要

BsonNull()与 MongoDB Scala 驱动程序结合使用,以查询 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 } 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

{ item : null } 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

{ item, BCON_NULL } 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

Eq("item", BsonNull.Value)使用 FilterDefinitionBuilder.Eq() 的 查询item 方法匹配包含值为 的 字段null 不包含item 字段的文档。

item => nil 查询将匹配包含值为 nilitem 字段或者不包含 item 字段的文档。

eq("item", null) 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

eq("item", null) 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

eq("item", null) 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

{ item : None } 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

{ item : null } 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

[ item => undef ] 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

{ item : None } 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

{ item => nil } 查询将匹配包含值为 nilitem 字段或者不包含 item 字段的文档。

equal("item", BsonNull) 查询将匹配包含值为 nullitem 字段或者不包含 item 字段的文档。

db.inventory.find( { item: 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()))

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

要查询存在且不为 null的字段,请使用{ $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):

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)查询匹配包含值为nullitem字段的文档;即item字段的值属于BSON 类型Null (BSON 类型10 :

type("item", BsonType.NULL)查询匹配包含值为nullitem字段的文档。这意味着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 } } )

将以下查询筛选器文档复制到查询栏中,然后单击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))

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

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

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

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

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

使用 FilterDefinitionBuilder.Exists() 方法的 Exists("item", 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.inventory.find( { item : { $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))

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

提示

另请参阅:

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

[1] 用户不能再将查询过滤器 $type: 0 用作 $exists:false 的同义词。要查询空字段或缺失字段,请参阅查询空字段或缺失字段。

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

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

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

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

  3. 如果 Clusters(数据库部署)页面尚未出现,请单击侧边栏中的 Database(数据库)。

    此时会显示“集群”页面。

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 } }

后退

项目成果