Docs Menu

null 또는 누락된 필드에 대한 쿼리

You can query for null or missing fields in MongoDB using the following methods:


오른쪽 상단의 언어 선택 드롭다운 메뉴를 사용하여 다음 예시의 언어를 설정하거나 MongoDB Compass를 선택합니다.


Different query operators in MongoDB treat null values differently.

This page provides examples of operations that query for null values using the db.collection.find() method in mongosh.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using MongoDB Compass

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using mongoc_collection_find_with_opts. .. include:: /includes/driver-examples/examples-intro.rst

This page provides examples of operations that query for null values using the MongoCollection.Find() method in the MongoDB C# Driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the Collection.Find function in the MongoDB Go Driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

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.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

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.

이 드라이버는 필터 문서를 쉽게 만들 수 있도록 com.mongodb.client.model.Filters 헬퍼 메서드를 제공합니다. 이 페이지의 예시에서는 이러한 방법을 사용하여 필터 문서를 생성합니다.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values by using the MongoCollection.find() method in the MongoDB Kotlin Coroutine Driver.

이 드라이버는 필터 문서를 쉽게 만들 수 있도록 com.mongodb.client.model.Filters 헬퍼 메서드를 제공합니다. 이 페이지의 예시에서는 이러한 방법을 사용하여 필터 문서를 생성합니다.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the motor.motor_asyncio.AsyncIOMotorCollection.find method in the Motor driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the Collection.find() method in the MongoDB Node.js Driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the MongoDB\\Collection::find() method in the MongoDB PHP Library.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the pymongo.collection.Collection.find method in the PyMongo Python driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the Mongo::Collection#find() method in the MongoDB Ruby Driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

This page provides examples of operations that query for null values using the collection.find() method in the MongoDB Scala Driver.

이 페이지의 예시에서는 inventory 컬렉션을 사용합니다. MongoDB 인스턴스의 테스트 데이터베이스에 연결한 다음 inventory 컬렉션을 만듭니다:

중요

다음을 사용하세요. BCON_NULL with the MongoDB C driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. BsonNull.Value with the MongoDB C# driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. nil with the MongoDB Go driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. null with the Kotlin Coroutine driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. None with the Motor driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. None with the PyMongo Python driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. nil with the MongoDB Ruby driver to query for null or missing fields in MongoDB.

중요

다음을 사용하세요. BsonNull() with the MongoDB Scala driver to query for null or missing fields in MongoDB.

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()

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 : null } query matches documents that either contain the item field whose value is null or that do not contain the item field.

The { item, BCON_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", 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 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 : 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 }
Query null value or missing field
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()))

The query returns both documents in the collection.

To query for fields that exist and are not null, use the { $ne : null } filter. The { item : { $ne : null } } query matches documents where the item field exists and has a non-null value.

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));

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 유형 Null (BSON Type 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 유형 Null (BSON Type 10):

The { item, { $type, BCON_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 유형 Null (BSON Type 10):

The Type("item", BsonType.Null) query using the FilterDefinitionBuilder.Type() method matches only documents that contain the item field whose value is null. The value of the item field is of BSON 유형 Null (BSON Type 10):

The following query matches only documents that contain the item field whose value is of BSON 유형 Null (BSON Type 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 유형 Null (BSON Type 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 유형 Null (BSON Type 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 유형 Null (BSON Type 10 :

The type("item", BsonType.NULL) query matches only documents that contain the item field whose value is null. This means the value of the item field is of BSON 유형 Null (BSON Type 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 유형 Null (BSON Type 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 유형 Null (BSON Type 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 유형 Null (BSON Type 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 유형 Null (BSON Type 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 유형 Null (BSON Type 10):

db.inventory.find( { item : { $type: 10 } } )

Copy the following query filter document into the query bar and click Find:

{ item : { $type: 10 } }
Find null type
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))

The query returns only the document where the item field has a value of 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:

The { item : { $exists: false } } query matches documents that do not contain the item field:

The { item, { $exists, BCON_BOOL (false) } } query matches documents that do not contain the item field:

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 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 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 } }
Query for null value
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))

The query only returns the document that does not contain the item field.

다음도 참조하세요.

Reference documentation for the $type and $exists operators.

[1] Users can no longer use the query filter $type: 0 as a synonym for $exists:false. To query for null or missing fields, see null 또는 누락된 필드에 대한 쿼리.

이 섹션의 예시에서는 샘플 교육 데이터세트를 사용합니다. MongoDB Atlas 배포에 샘플 데이터세트를 로드하는 방법을 알아보려면 샘플 데이터 로드를 참조하세요.

To query for a null or missing field in MongoDB Atlas, follow these steps:

1
  1. 아직 표시되지 않은 경우 탐색 표시줄의 Organizations 메뉴에서 원하는 프로젝트가 포함된 조직을 선택합니다.

  2. 아직 표시되지 않은 경우 내비게이션 바의 Projects 메뉴에서 프로젝트를 선택합니다.

  3. 아직 표시되지 않은 경우 사이드바에서 Clusters를 클릭합니다.

    Clusters(클러스터) 페이지가 표시됩니다.

2
  1. 샘플 데이터가 포함된 클러스터의 경우 Browse Collections 을 클릭합니다.

  2. 왼쪽 탐색 창에서 sample_training 데이터베이스를 선택합니다.

  3. companies 컬렉션을 선택합니다.

3

Click the Insert Document button to display the dialog box, and then click Insert to insert a document with only the _id field.

4

To find a document that contains a null or missing value, specify a query filter document in the Filter field. A query filter document uses query operators to specify search conditions.

Different query operators in MongoDB treat null values differently. To apply a query filter, copying each of the following documents into the Filter search bar and click Apply.

Use the following query filter to match documents that either contain a description field with a null value or do not contain the description field:

{ description : null }

Use the following query filter to match only documents that contain a description field with a null value. This filter specifies that the value of the field must be BSON 유형 Null (BSON Type 10):

{ description : { $type: 10 } }

Use the following query filter to match only documents that do not contain the description field. Only the document that you inserted earlier should appear:

{ description : { $exists: false } }