文档菜单
文档首页
/
MongoDB 手册
/ /

查询嵌套/嵌套文档

本页内容

  • 使用点符号查询嵌套字段
  • 匹配嵌套/嵌套文档
  • 使用 MongoDB Atlas 查询嵌套文档
  • 其他查询教程

您可以通过以下方法在 MongoDB 中查询嵌套文档:

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

  • MongoDB Atlas UI。了解更多信息,请参阅 使用 MongoDB Atlas 查询嵌套文档

  • MongoDB Compass.


使用右上角的 选择您的语言 下拉菜单设置以下示例或 MongoDB Compass 的语言。


本页提供了以下示例:对嵌入/嵌套文档的查询操作使用db.collection.find() 方法在 mongosh.

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用 MongoDB Compass.

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用 mongoc_collection_find_with_opts.

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用MongoCollection.Find()方法在MongoDB C# Driver中。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用Collection.Find函数在MongoDB Go Driver中。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用MongoDB的com.mongodb.reactivestreams.client.MongoCollection.find方法在Java Reactive Streams Driver中。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用MongoDB com.mongodb.client.MongoCollection.find 方法,在MongoDB Java 同步驱动程序。

提示

驱动程序提供了 com.mongodb.client.model.Filters 辅助方法,以简化过滤文档的创建。本页上的示例使用这些方法创建过滤文档。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作通过使用MongoDB MongoCollection.find() 方法在 MongoDB Kotlin Coroutine Driver.

提示

该驱动程序提供 com.mongodb.client.model.Filters 辅助方法来简化过滤器文档的创建。本页面上的示例使用这些方法创建过滤器文档。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用motor.motor_asyncio.AsyncIOMotorCollection.find方法在Motor驱动程序中。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用 Collection.find() 方法在 MongoDB Node.js 驱动程序。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用 MongoDB::Collection::find() 方法在 MongoDB Perl 驱动程序 中。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用MongoDB PHP库中的 MongoDB\\Collection::find() 方法。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用PyMongo Python驱动中的 pymongo.collection.Collection.find 方法。 方法。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用MongoDB Ruby驱动中的 Mongo::Collection#find() 方法。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了以下示例:对嵌入/嵌套文档的查询操作使用MongoDB Scala驱动中的 collection.find() 方法。

本页的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

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查询栏中并点击查找:

{ "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>))

以下查询使用小于操作符($lt)在size字段中嵌套的字段h上。

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

将以下过滤条件复制到Compass查询栏中,然后点击查找

{ "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))

以下查询选择所有嵌套字段h小于15、嵌套字段uom等于"in"和字段status等于"D"的文档。

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

将以下过滤条件复制到Compass查询栏中,然后点击查找

{ "size.h": { $lt: 15 }, "size.uom": "in", status: "D" }
Query multiple nested fields
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查询栏中,然后点击查找

{ size: { h: 14, w: 21, uom: "cm" } }
Query embedded field
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")))

警告

MongoDB 不建议在嵌入式文档上进行比较,因为这些操作需要指定 <value> 文档的 精确 匹配,包括字段顺序。

例如,以下查询在 inventory 集合中没有匹配任何文档

db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
Query embedded field
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部署中的信息,请参阅加载数据。

要在MongoDB Atlas中查询嵌套文档,请按照以下步骤操作

1
  1. 如果它还没有显示,请从导航栏中的 组织菜单选择包含您所需项目的组织

  2. 如果它还没有显示,请从导航栏中的项目菜单中选择您的项目。

  3. 如果它还没有显示,请点击侧边栏中的集群

    将显示集群页面。

2
  1. 对于包含样本数据的集群,请点击浏览集合

  2. 在左侧导航面板中,选择 sample_mflix 数据库。

  3. 选择 movies 集合。

3

过滤器 字段中指定 查询过滤器文档。查询过滤器文档使用 查询运算符来指定搜索条件。

将以下查询过滤器文档复制到 过滤器 搜索框

{ "awards.wins": 1 }
4

此查询过滤器返回 sample_mflix.movies 集合中所有 awards 字段的嵌入式文档包含 { wins: 1 } 的文档。

查看更多查询示例,请参阅

返回

查询