문서 메뉴
문서 홈
/
MongoDB 매뉴얼
/ /

내장된 또는 중첩된 문서에 대한 쿼리

이 페이지의 내용

  • 점 표기법이 있는 중첩 필드 쿼리
  • 포함/중첩 문서 일치
  • MongoDB Atlas로 포함된 문서 쿼리하기
  • 추가 쿼리 튜토리얼

다음 메서드를 사용하여 MongoDB에서 문서를 쿼리할 수 있습니다.

  • 프로그래밍 언어의 드라이버입니다.

  • MongoDB Atlas UI. 자세한 내용은 MongoDB Atlas로 내장된 문서 쿼리하기에서 확인 가능합니다.

  • MongoDB Compass


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


이 페이지에서는mongosh 에서 db.collection.find() 메서드를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다

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

이 페이지에서는 MongoDB Compass를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다.

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

이 페이지에서는 mongoc_collection_find_with_opts를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다.

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

이 페이지는 MongoDB C# 드라이버에서 MongoCollection.Find()를 사용해 내장된/중첩된 문서에서 쿼리 작업을 실행하는 예시를 설명합니다.

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

이 페이지에서는 Collection.Find 를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다. MongoDB Go 드라이버 의 함수 .

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

이 페이지에서는 com.mongodb.reactivestreams.client.MongoCollection.find MongoDB Java Reactive Streams 드라이버.

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

이 페이지는 Java Synchronous 드라이버에서 com.mongodb.client.MongoCollection.find 메서드를 사용해 내장된/중첩된 문서에서 쿼리 작업을 실행하는 예시를 설명합니다.

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

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

이 페이지에서는 MongoCollection.find() 메서드 를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다. MongoDB Kotlin 코루틴 드라이버의 메서드입니다.

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

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

이 페이지에서는 Motor 드라이버에서 motor.motor_asyncio.AsyncIOMotorCollection.find 메서드를 사용하여 포함/중첩된 문서 배열에 대한 쿼리 작업의 예시를 보여줍니다.

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

이 페이지에서는 Collection.find() 메서드를 사용하여 포함/중첩 문서에서 수행하는 쿼리 작업의 예시를 제공합니다. MongoDB Node.js 드라이버에서 해당 작업을 수행합니다.

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

이 페이지에서는 MongoDB::Collection::find() 메서드 를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다. MongoDB 펄(Perl) 드라이버 의 메서드.

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

이 페이지에서는 MongoDB PHP 라이브러리 MongoDB\\Collection::find() 에서 메서드를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다.

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

이 페이지에서는 를 사용하여 포함된/중첩된 문서에 대한 쿼리 작업의 pymongo.collection.Collection.find 예를 제공합니다. 메서드 를 제공합니다.

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

이 페이지에서는 MongoDB Ruby 드라이버에서 Mongo::Collection#find() 메서드를 사용하여 포함/중첩 문서에 대한 쿼리 작업의 예시를 제공합니다.

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

이 페이지에서는 collection.find() 메서드를 사용하여 내장된/중첩된 문서에 대한 쿼리 작업의 예를 제공합니다. MongoDB 스칼라 드라이버 의 메서드 .

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

db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
[
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]

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 (
"item", BCON_UTF8 ("journal"),
"qty", BCON_INT64 (25),
"size", "{",
"h", BCON_DOUBLE (14),
"w", BCON_DOUBLE (21),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("A"));
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 (
"item", BCON_UTF8 ("notebook"),
"qty", BCON_INT64 (50),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"status", BCON_UTF8 ("A"));
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 (
"item", BCON_UTF8 ("paper"),
"qty", BCON_INT64 (100),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"status", BCON_UTF8 ("D"));
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 (
"item", BCON_UTF8 ("planner"),
"qty", BCON_INT64 (75),
"size", "{",
"h", BCON_DOUBLE (22.85),
"w", BCON_DOUBLE (30),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("D"));
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 (
"item", BCON_UTF8 ("postcard"),
"qty", BCON_INT64 (45),
"size", "{",
"h", BCON_DOUBLE (10),
"w", BCON_DOUBLE (15.25),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("A"));
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
{
{ "item", "journal" },
{ "qty", 25 },
{ "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } },
{ "status", "A" }
},
new BsonDocument
{
{ "item", "notebook" },
{ "qty", 50 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "status", "A" }
},
new BsonDocument
{
{ "item", "paper" },
{ "qty", 100 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "planner" },
{ "qty", 75 },
{ "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm" } } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "postcard" },
{ "qty", 45 },
{ "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm" } } },
{ "status", "A" } },
};
collection.InsertMany(documents);
docs := []interface{}{
bson.D{
{"item", "journal"},
{"qty", 25},
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
{"status", "A"},
},
bson.D{
{"item", "notebook"},
{"qty", 50},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"status", "A"},
},
bson.D{
{"item", "paper"},
{"qty", 100},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"status", "D"},
},
bson.D{
{"item", "planner"},
{"qty", 75},
{"size", bson.D{
{"h", 22.85},
{"w", 30},
{"uom", "cm"},
}},
{"status", "D"},
},
bson.D{
{"item", "postcard"},
{"qty", 45},
{"size", bson.D{
{"h", 10},
{"w", 15.25},
{"uom", "cm"},
}},
{"status", "A"},
},
}
result, err := coll.InsertMany(context.TODO(), docs)
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
collection.insertMany(
listOf(
Document("item", "journal")
.append("qty", 25)
.append("size", Document("h", 14)
.append("w", 21)
.append("uom", "cm")
)
.append("status", "A"),
Document("item", "notebook")
.append("qty", 50)
.append("size", Document("h", 8.5)
.append("w", 11)
.append("uom", "in")
)
.append("status", "A"),
Document("item", "paper")
.append("qty", 100)
.append("size", Document("h", 8.5)
.append("w", 11)
.append("uom", "in")
)
.append("status", "D"),
Document("item", "planner")
.append("qty", 75)
.append("size", Document("h", 22.85)
.append("w", 30)
.append("uom", "cm")
)
.append("status", "D"),
Document("item", "postcard")
.append("qty", 45)
.append("size", Document("h", 10)
.append("w", 15.25)
.append("uom", "cm")
)
.append("status", "A"),
)
)
# Subdocument key order matters in a few of these examples so we have
# to use bson.son.SON instead of a Python dict.
from bson.son import SON
await db.inventory.insert_many(
[
{
"item": "journal",
"qty": 25,
"size": SON([("h", 14), ("w", 21), ("uom", "cm")]),
"status": "A",
},
{
"item": "notebook",
"qty": 50,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"status": "A",
},
{
"item": "paper",
"qty": 100,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"status": "D",
},
{
"item": "planner",
"qty": 75,
"size": SON([("h", 22.85), ("w", 30), ("uom", "cm")]),
"status": "D",
},
{
"item": "postcard",
"qty": 45,
"size": SON([("h", 10), ("w", 15.25), ("uom", "cm")]),
"status": "A",
},
]
)
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
}
]);
# Subdocument key order matters in this example so we have
# to use Tie::IxHash instead of a regular, unordered Perl hash.
$db->coll("inventory")->insert_many(
[
{
item => "journal",
qty => 25,
size => Tie::IxHash->new( h => 14, w => 21, uom => "cm" ),
status => "A"
},
{
item => "notebook",
qty => 50,
size => Tie::IxHash->new( h => 8.5, w => 11, uom => "in" ),
status => "A"
},
{
item => "paper",
qty => 100,
size => Tie::IxHash->new( h => 8.5, w => 11, uom => "in" ),
status => "D"
},
{
item => "planner",
qty => 75,
size => Tie::IxHash->new( h => 22.85, w => 30, uom => "cm" ),
status => "D"
},
{
item => "postcard",
qty => 45,
size => Tie::IxHash->new( h => 10, w => 15.25, uom => "cm" ),
status => "A"
}
]
);
$insertManyResult = $db->inventory->insertMany([
[
'item' => 'journal',
'qty' => 25,
'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
'status' => 'A',
],
[
'item' => 'notebook',
'qty' => 50,
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
'status' => 'A',
],
[
'item' => 'paper',
'qty' => 100,
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
'status' => 'D',
],
[
'item' => 'planner',
'qty' => 75,
'size' => ['h' => 22.85, 'w' => 30, 'uom' => 'cm'],
'status' => 'D',
],
[
'item' => 'postcard',
'qty' => 45,
'size' => ['h' => 10, 'w' => 15.25, 'uom' => 'cm'],
'status' => 'A',
],
]);
# Subdocument key order matters in a few of these examples so we have
# to use bson.son.SON instead of a Python dict.
from bson.son import SON
db.inventory.insert_many(
[
{
"item": "journal",
"qty": 25,
"size": SON([("h", 14), ("w", 21), ("uom", "cm")]),
"status": "A",
},
{
"item": "notebook",
"qty": 50,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"status": "A",
},
{
"item": "paper",
"qty": 100,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"status": "D",
},
{
"item": "planner",
"qty": 75,
"size": SON([("h", 22.85), ("w", 30), ("uom", "cm")]),
"status": "D",
},
{
"item": "postcard",
"qty": 45,
"size": SON([("h", 10), ("w", 15.25), ("uom", "cm")]),
"status": "A",
},
]
)
client[:inventory].insert_many([
{ item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A' },
{ item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A' },
{ item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D' },
{ item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D' },
{ item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A' }
])
collection.insertMany(Seq(
Document("""{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""),
Document("""{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" }"""),
Document("""{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }"""),
Document("""{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }"""),
Document("""{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }""")
)).execute()

포함된/중첩된 문서의 필드에 쿼리 조건을 지정하려면 점 표기법("field.nestedField")을 사용합니다.

참고

점 표기법을 사용하여 쿼리하는 경우 필드와 중첩 필드는 따옴표 안에 포함되어야 합니다.

다음 예시에서는 size 필드에 중첩된 uom 필드가 "in"와 동등한 모든 문서를 선택합니다.

db.inventory.find( { "size.uom": "in" } )

다음 필터를 Compass 쿼리 표시줄에 복사하고 Find 을(를) 클릭합니다.

{ "size.uom": "in" }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("size.uom", BCON_UTF8 ("in"));
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("size.uom", "in");
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{{"size.uom", "in"}},
)
findPublisher = collection.find(eq("size.uom", "in"));
findIterable = collection.find(eq("size.uom", "in"));
val findFlow = collection
.find(eq("size.uom", "in"))
cursor = db.inventory.find({"size.uom": "in"})
const cursor = db.collection('inventory').find({
'size.uom': 'in'
});
$cursor = $db->coll("inventory")->find( { "size.uom" => "in" } );
$cursor = $db->inventory->find(['size.uom' => 'in']);
cursor = db.inventory.find({"size.uom": "in"})
client[:inventory].find('size.uom' => 'in')
findObservable = collection.find(equal("size.uom", "in"))

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1>: { <operator1>: <value1> }, ... }

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1>: { <operator1>: <value1> }, ... }

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1>: { <operator1>: <value1> }, ... }

MongoDB는 동등 필터 외에도 필터 조건을 지정하는 다양한 쿼리 연산자 를 제공합니다.FilterDefinitionBuilder 사용 메서드를 사용하여 필터 문서를 만듭니다. 예를 들면 다음과 같습니다.

var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));

MongoDB는 동등 조건 외에도 필터 조건을 지정하는 다양한 쿼리 연산자 를 제공합니다. com.mongodb.client.model.Filters 필터 문서를 쉽게 만들 수 있는 헬퍼 메서드. 예를 들면 다음과 같습니다.

and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))

MongoDB는 동등 조건 외에도 필터 조건을 지정하는 다양한 쿼리 연산자 를 제공합니다. com.mongodb.client.model.Filters 필터 문서를 쉽게 만들 수 있는 헬퍼 메서드. 예를 들면 다음과 같습니다.

and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))

MongoDB는 동등 조건 외에도 필터 조건을 지정하는 다양한 쿼리 연산자 를 제공합니다. com.mongodb.client.model.Filters 필터 문서를 쉽게 만들 수 있는 헬퍼 메서드. 예를 들면 다음과 같습니다.

and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1>: { <operator1>: <value1> }, ... }

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1>: { <operator1>: <value1> }, ... }

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1> => { <operator1> => <value1> }, ... }

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

[ <field1> => [ <operator1> => <value1> ], ... ]

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1>: { <operator1>: <value1> }, ... }

쿼리 필터 문서에서는 쿼리 연산자를 사용하여 다음 형식의 조건을 지정할 수 있습니다.

{ <field1> => { <operator1> => <value1> }, ... }

MongoDB는 동등 조건 외에도 필터 조건을 지정하는 다양한 쿼리 연산자 를 제공합니다. com.mongodb.client.model.Filters_ 헬퍼 메서드를 사용하여 필터 문서를 쉽게 만들 수 있습니다. 예를 들면 다음과 같습니다.

and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))

다음 쿼리는 size 필드에 포함된 h 필드에서 '보다 작음'(less than) 연산자($lt)를 사용합니다.

db.inventory.find( { "size.h": { $lt: 15 } } )

다음 필터를 Compass 쿼리 표시줄에 복사하고 Find를 클릭합니다.

{ "size.h": { $lt: 15 } }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"size.h", "{",
"$lt", BCON_INT64 (15),
"}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Lt("size.h", 15);
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"size.h", bson.D{
{"$lt", 15},
}},
})
findPublisher = collection.find(lt("size.h", 15));
findIterable = collection.find(lt("size.h", 15));
val findFlow = collection
.find(lt("size.h", 15))
cursor = db.inventory.find({"size.h": {"$lt": 15}})
const cursor = db.collection('inventory').find({
'size.h': { $lt: 15 }
});
$cursor = $db->coll("inventory")->find( { "size.h" => { '$lt' => 15 } } );
$cursor = $db->inventory->find(['size.h' => ['$lt' => 15]]);
cursor = db.inventory.find({"size.h": {"$lt": 15}})
client[:inventory].find('size.h' => { '$lt' => 15 })
findObservable = collection.find(lt("size.h", 15))

다음 쿼리는 중첩된 필드 h15보다 작고, 중첩된 필드 uom"in"와 같으며, status 필드가 "D"인 모든 문서를 선택합니다.

db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )

다음 필터를 Compass 쿼리 표시줄에 복사하고 Find를 클릭합니다.

{ "size.h": { $lt: 15 }, "size.uom": "in", status: "D" }
여러 중첩 필드 쿼리
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"size.h", "{",
"$lt", BCON_INT64 (15),
"}",
"size.uom", BCON_UTF8 ("in"),
"status", BCON_UTF8 ("D"));
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(builder.Lt("size.h", 15), builder.Eq("size.uom", "in"), builder.Eq("status", "D"));
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"size.h", bson.D{
{"$lt", 15},
}},
{"size.uom", "in"},
{"status", "D"},
})
findPublisher = collection.find(and(
lt("size.h", 15),
eq("size.uom", "in"),
eq("status", "D")
));
findIterable = collection.find(and(
lt("size.h", 15),
eq("size.uom", "in"),
eq("status", "D")
));
val findFlow = collection
.find(and(
lt("size.h", 15),
eq("size.uom", "in"),
eq("status", "D")
))
cursor = db.inventory.find({"size.h": {"$lt": 15}, "size.uom": "in", "status": "D"})
const cursor = db.collection('inventory').find({
'size.h': { $lt: 15 },
'size.uom': 'in',
status: 'D'
});
$cursor = $db->coll("inventory")->find(
{ "size.h" => { '$lt' => 15 }, "size.uom" => "in", status => "D" }
);
$cursor = $db->inventory->find([
'size.h' => ['$lt' => 15],
'size.uom' => 'in',
'status' => 'D',
]);
cursor = db.inventory.find({"size.h": {"$lt": 15}, "size.uom": "in", "status": "D"})
client[:inventory].find('size.h' => { '$lt' => 15 },
'size.uom' => 'in',
'status' => 'D')
findObservable = collection.find(and(
lt("size.h", 15),
equal("size.uom", "in"),
equal("status", "D")
))

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field>: <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field>: <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field>: <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동등 조건을 지정하려면 Eq 를 사용하여 필터를 구성합니다.<value> 메서드에서 는 일치시킬 문서입니다.

Builders<BsonDocument>.Filter.Eq(<field>, <value>)

임베디드/중첩 문서인 필드에 동등성 조건을 지정하려면 필터 문서인 eq( <field1>, <value>)을(를) 사용하세요. 여기서 <value>은(는) 일치하는 문서입니다.

임베디드/중첩 문서인 필드에 동등성 조건을 지정하려면 필터 문서인 eq( <field1>, <value>)을(를) 사용하세요. 여기서 <value>은(는) 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field>: <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field>: <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field> => <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 [ <field> => <value> ]를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field>: <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

포함된/중첩된 문서인 필드에 동일성 조건을 지정하려면 쿼리 필터 문서 { <field> => <value> }를 사용하세요. 여기서 <value>은 일치하는 문서입니다.

임베디드/중첩 문서인 필드에 동등성 조건을 지정하려면 필터 문서인 equal( <field1>, <value> )을(를) 사용하세요. 여기서 <value>은(는) 일치하는 문서입니다.

예를 들어, 다음 쿼리는 size 필드가 { h: 14, w: 21, uom: "cm" } 문서와 동일한 모든 문서를 선택합니다.

db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )

다음 필터를 Compass 쿼리 표시줄에 복사하고 Find를 클릭합니다.

{ size: { h: 14, w: 21, uom: "cm" } }
포함된 필드 쿼리
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"size", "{",
"h", BCON_DOUBLE (14),
"w", BCON_DOUBLE (21),
"uom", BCON_UTF8 ("cm"),
"}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } });
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
})
FindPublisher<Document> findPublisher = collection.find(eq("size", Document.parse("{ h: 14, w: 21, uom: 'cm' }")));
FindIterable<Document> findIterable = collection.find(eq("size", Document.parse("{ h: 14, w: 21, uom: 'cm' }")));
val findFlow = collection
.find(eq("size", Document.parse("{ h: 14, w: 21, uom: 'cm' }")))
cursor = db.inventory.find({"size": SON([("h", 14), ("w", 21), ("uom", "cm")])})
const cursor = db.collection('inventory').find({
size: { h: 14, w: 21, uom: 'cm' }
});
# Subdocument key order matters in this example so we have
# to use Tie::IxHash instead of a regular, unordered Perl hash.
$cursor = $db->coll("inventory")->find(
{ size => Tie::IxHash->new( h => 14, w => 21, uom => "cm" ) }
);
$cursor = $db->inventory->find(['size' => ['h' => 14, 'w' => 21, 'uom' => 'cm']]);
cursor = db.inventory.find({"size": SON([("h", 14), ("w", 21), ("uom", "cm")])})
client[:inventory].find(size: { h: 14, w: 21, uom: 'cm' })
var findObservable = collection.find(equal("size", Document("h" -> 14, "w" -> 21, "uom" -> "cm")))

경고

작업에는 필드 <value> 순서를 포함하여 지정된 문서와 정확히 일치해야 하므로 MongoDB는 내장된 문서에 대한 비교 를 권장하지 않습니다.

예를 들어 다음 쿼리는 inventory 컬렉션의 어떤 문서와도 일치하지 않습니다.

db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
포함된 필드 쿼리
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"size", "{",
"w", BCON_DOUBLE (21),
"h", BCON_DOUBLE (14),
"uom", BCON_UTF8 ("cm"),
"}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("size", new BsonDocument { { "w", 21 }, { "h", 14 }, { "uom", "cm" } });
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"size", bson.D{
{"w", 21},
{"h", 14},
{"uom", "cm"},
}},
})
findPublisher = collection.find(eq("size", Document.parse("{ w: 21, h: 14, uom: 'cm' }")));
findIterable = collection.find(eq("size", Document.parse("{ w: 21, h: 14, uom: 'cm' }")));
val findFlow = collection
.find(eq("size", Document.parse("{ w: 21, h: 14, uom: 'cm' }")))
cursor = db.inventory.find({"size": SON([("w", 21), ("h", 14), ("uom", "cm")])})
const cursor = db.collection('inventory').find({
size: { w: 21, h: 14, uom: 'cm' }
});
# Subdocument key order matters in this example so we have
# to use Tie::IxHash instead of a regular, unordered Perl hash.
$cursor = $db->coll("inventory")->find(
{ size => Tie::IxHash->new( w => 21, h => 14, uom => "cm" ) }
);
$cursor = $db->inventory->find(['size' => ['w' => 21, 'h' => 14, 'uom' => 'cm']]);
cursor = db.inventory.find({"size": SON([("w", 21), ("h", 14), ("uom", "cm")])})
client[:inventory].find(size: { h: 21, w: 14, uom: 'cm' })
findObservable = collection.find(equal("size", Document("w" -> 21, "h" -> 14, "uom" -> "cm")))

내장된 문서에 비교를 사용하는 쿼리를 쿼리 표현에 정렬된 데이터 구조를 사용하지 않는 드라이버와 함께 사용할 경우 예기치 않은 동작이 발생할 수 있습니다.

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

다음 단계를 수행하여 MongoDBDB Atlas에서 포함된 문서를 쿼리할 수 있습니다.

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

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

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

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

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

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

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

3

Filter 필드에 쿼리 필터 문서를 지정합니다. 쿼리 필터 문서는 쿼리 연산자를 사용하여 검색 조건을 지정합니다.

다음 쿼리 필터 문서를 Filter 검색 바에 복사합니다.

{ "awards.wins": 1 }
4

이 쿼리 필터는 sample_mflix.movies 컬렉션에서 awards 필드의 포함된 문서 중 { wins: 1 }에 해당하는 모든 문서를 반환합니다.

추가 쿼리 예시는 아래에서 확인 가능합니다.

돌아가기

쿼리

다음

배열