选择语言
On this page本页内容
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.使用右上角的“选择语言”下拉菜单设置以下示例的语言。
Different query operators in MongoDB treat MongoDB中的不同查询运算符对null
values differently.null
值的处理方式不同。
This page provides examples of operations that query for 本页提供了使用null
values using the db.collection.find()
method in mongosh
. mongosh
中的db.collection.find()
方法查询空值的操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of operations that query for 本页提供使用MongoDB Compass查询null
values using MongoDB Compass. null
值的操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。Populate the 使用以下文档填充inventory
collection with the following documents:inventory
集合:
This page provides examples of operations that query for 本页提供了使用MongoDB C#驱动程序中的null
values using the MongoCollection.Find() method in the MongoDB C# Driver. MongoCollection.Find()
方法查询空值的操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of operations that query for null
values using the Collection.Find function in the MongoDB Go Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of operations that query for null
values using the motor.motor_asyncio.AsyncIOMotorCollection.find
method in the Motor driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for 本页提供了使用MongoDB Node.js驱动程序中的null
values using the Collection.find() method in the MongoDB Node.js Driver. Collection.find()
方法查询空值的操作示例。The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To populate the 要填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page provides examples of operations that query for null
values using the MongoDB::Collection::find() method in the MongoDB Perl Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the MongoDB\\Collection::find()
method in the MongoDB PHP Library. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the pymongo.collection.Collection.find
method in the PyMongo Python driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the Mongo::Collection#find() method in the MongoDB Ruby Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of operations that query for null
values using the collection.find() method in the MongoDB Scala Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
Use 使用BsonNull.Value
with the MongoDB C# driver to query for null
or missing fields in MongoDB.BsonNull.Value
和MongoDB C#驱动程序来查询MongoDB中的null
或缺失字段。
Use nil
with the MongoDB Go driver to query for null
or missing fields in MongoDB.
Use None
with the Motor driver to query for null
or missing fields in MongoDB.
Use undef
with the MongoDB Perl driver to query for null
or missing fields in MongoDB.
Use None
with the PyMongo Python driver to query for null
or missing fields in MongoDB.
Use nil
with the MongoDB Ruby driver to query for null
or missing fields in MongoDB.
Use BsonNull()
with the MongoDB Scala driver to query for null
or missing fields in MongoDB.
db.inventory.insertMany([ { _id: 1, item: null }, { _id: 2 } ])
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
[ { "_id": 1, "item": null }, { "_id": 2 } ]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.有关在MongoDB Compass中插入文档的说明,请参阅插入文档。
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}") ));
await db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);
$db->coll("inventory")->insert_many( [ { _id => 1, item => undef }, { _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()
The { item : null }
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.{ item : null }
查询匹配包含值为null
的item
字段或不包含item
字段的文档。
The { item : null }
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.{ item : null }
查询匹配包含值为null
的项字段或不包含项字段的文档。
The Eq("item", BsonNull.Value)
query using the FilterDefinitionBuilder.Eq() method matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The item => nil
query matches documents that either contain the item
field whose value is nil
or that do not contain the item
field.
The eq("item", null)
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The eq("item", null)
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The { item : None }
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The { item : null }
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The { item => undef }
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The [ item => undef ]
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The { item : None }
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
The { item => nil }
query matches documents that either contain the item
field whose value is nil
or that do not contain the item
field.
The equal("item", BsonNull)
query matches documents that either contain the item
field whose value is null
or that do not contain the item
field.
db.inventory.find( { item: null } )
Copy the following query filter document into the query bar and click Find:将以下查询筛选器文档复制到查询栏中,然后单击“查找”:
{ item: 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));
cursor = db.inventory.find({"item": None})
const cursor = db.collection('inventory').find({ item: null });
$cursor = $db->coll("inventory")->find( { item => undef } );
$cursor = $db->inventory->find(['item' => null]);
cursor = db.inventory.find({"item": None})
client[:inventory].find(item: nil)
var findObservable = collection.find(equal("item", BsonNull()))
The query returns both documents in the collection.查询返回集合中的两个文档。
The {item:{{ item : { $type: 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :$type
:10}}查询仅匹配包含值为空的item字段的文档;即,项目字段的值为BSON类型Null
(类型编号10):
The { item : { $type: 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :{ item : { $type: 10 } }
查询仅匹配包含值为null
的item
字段的文档;即,item
字段的值为BSON类型Null
(类型编号10
):
The Type("item", BsonType.Null)
query using the FilterDefinitionBuilder.Type() method matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The following query matches only documents that contain the 以下查询仅匹配包含值为BSON类型item
field whose value is of BSON Type Null
(type number 10
) :Null
(类型号10)的item
字段的文档:
The type("item", BsonType.NULL)
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The type("item", BsonType.NULL)
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The type("item", BsonType.NULL)
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The { item => { $type => 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The [ item => [ $type => 10 ] ]
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The { item : { $type: 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
The { item => { $type => 10 } }
query matches only documents that contain the item
field whose value is null
; i.e. the value of the item
field is of BSON Type Null
(type number 10
) :
db.inventory.find( { item : { $type: 10 } } )
Copy the following query filter document into the query bar and click Find:将以下查询筛选器文档复制到查询栏中,然后单击“查找”:
{ item : { $type: 10 } }
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));
cursor = db.inventory.find({"item": {"$type": 10}})
const cursor = db.collection('inventory').find({ item: { $type: 10 } });
$cursor = $db->coll("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))
The query returns only the document where the 查询仅返回item
field has a value of null
.item
字段值为null
的文档。
The following example queries for documents that do not contain a field. 以下示例查询不包含字段的文档。[1]
The { item : { $exists: false } }
query matches documents that do not contain the item
field:{ item : { $exists: false } }
查询与不包含item
字段的文档匹配:
The { item : { $exists: false } }
query matches documents that do not contain the item
field:{ item : { $exists: false } }
查询与不包含item
字段的文档匹配:
The Exists("item", false)
query using the FilterDefinitionBuilder.Exists() method matches documents that do not contain the item
field:
The exists("item", false)
query matches documents that do not contain the item
field:
The exists("item", false)
query matches documents that do not contain the item
field:
The { item : { $exists: False } }
query matches documents that do not contain the item
field:
The { item : { $exists: false } }
query matches documents that do not contain the item
field:
The { item => { $exists => false } }
query matches documents that do not contain the item
field:
The [ item => [ $exists => false ] ]
query matches documents that do not contain the item
field:
The { item : { $exists: False } }
query matches documents that do not contain the item
field:
The { item => { $exists => false } }
query matches documents that do not contain the item
field:
The exists("item", exists = false)
query matches documents that do not contain the item
field:
db.inventory.find( { item : { $exists: false } } )
Copy the following query filter document into the query bar and click Find:将以下查询筛选器文档复制到查询栏中,然后单击“查找”:
{ item : { $exists: false } }
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));
cursor = db.inventory.find({"item": {"$exists": False}})
const cursor = db.collection('inventory').find({ item: { $exists: false } });
# For boolean values, use boolean.pm for 'true' and 'false' $cursor = $db->coll("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))
The query only returns the document that does not contain the 查询只返回不包含item
field.item
字段的文档。
[1] | $type: 0 as a synonym for $exists:false . $type:0 作为$exists:false 的同义词。 |