查询数组
您可以使用以下方法在 MongoDB 中查询数组
您编程语言的驱动程序。
请选择语言MongoDB Atlas UI。要了解更多信息,请参阅使用 MongoDB Atlas 查询数组。
➤使用右上角的选择您的语言下拉菜单设置以下示例的语言或选择 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 驱动程序。
提示
驱动程序提供了 com.mongodb.client.model.Filters 辅助方法,以简化过滤器文档的创建。本页面上的示例使用这些方法创建过滤器文档。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用motor.motor_asyncio.AsyncIOMotorCollection.find
方法在Motor驱动程序中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用Collection.find()方法在MongoDB Node.js Driver.中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用MongoDB::Collection::find()方法,该方法在MongoDB Perl Driver中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用MongoDB\\Collection::find()
方法,该方法在MongoDB PHP Library中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用pymongo.collection.Collection.find
方法,该方法在PyMongo Python驱动程序中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用Mongo::Collection#find()方法,该方法在MongoDB Ruby Driver.中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
本页提供使用查询数组字段的示例使用 collection.find() 方法在 MongoDB Scala Driver 中。
本页上的示例使用 inventory
集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass。
db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] }, { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] }, { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] }, { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] }, { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] } ]);
[ { "item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [ 14, 21 ] }, { "item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [ 14, 21 ] }, { "item": "paper", "qty": 100, "tags": ["red", "blank", "plain"], "dim_cm": [ 14, 21 ] }, { "item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [ 22.85, 30 ] }, { "item": "postcard", "qty": 45, "tags": ["blue"], "dim_cm": [ 10, 15.25 ] } ]
有关在 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), "tags", "[", BCON_UTF8 ("blank"), BCON_UTF8 ("red"), "]", "dim_cm", "[", BCON_INT64 (14), BCON_INT64 (21), "]"); 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), "tags", "[", BCON_UTF8 ("red"), BCON_UTF8 ("blank"), "]", "dim_cm", "[", BCON_INT64 (14), BCON_INT64 (21), "]"); 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), "tags", "[", BCON_UTF8 ("red"), BCON_UTF8 ("blank"), BCON_UTF8 ("plain"), "]", "dim_cm", "[", BCON_INT64 (14), BCON_INT64 (21), "]"); 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), "tags", "[", BCON_UTF8 ("blank"), BCON_UTF8 ("red"), "]", "dim_cm", "[", BCON_DOUBLE (22.85), BCON_INT64 (30), "]"); 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), "tags", "[", BCON_UTF8 ("blue"), "]", "dim_cm", "[", BCON_INT64 (10), BCON_DOUBLE (15.25), "]"); 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 }, { "tags", new BsonArray { "blank", "red" } }, { "dim_cm", new BsonArray { 14, 21 } } }, new BsonDocument { { "item", "notebook" }, { "qty", 50 }, { "tags", new BsonArray { "red", "blank" } }, { "dim_cm", new BsonArray { 14, 21 } } }, new BsonDocument { { "item", "paper" }, { "qty", 100 }, { "tags", new BsonArray { "red", "blank", "plain" } }, { "dim_cm", new BsonArray { 14, 21 } } }, new BsonDocument { { "item", "planner" }, { "qty", 75 }, { "tags", new BsonArray { "blank", "red" } }, { "dim_cm", new BsonArray { 22.85, 30 } } }, new BsonDocument { { "item", "postcard" }, { "qty", 45 }, { "tags", new BsonArray { "blue" } }, { "dim_cm", new BsonArray { 10, 15.25 } } } }; collection.InsertMany(documents);
docs := []interface{}{ bson.D{ {"item", "journal"}, {"qty", 25}, {"tags", bson.A{"blank", "red"}}, {"dim_cm", bson.A{14, 21}}, }, bson.D{ {"item", "notebook"}, {"qty", 50}, {"tags", bson.A{"red", "blank"}}, {"dim_cm", bson.A{14, 21}}, }, bson.D{ {"item", "paper"}, {"qty", 100}, {"tags", bson.A{"red", "blank", "plain"}}, {"dim_cm", bson.A{14, 21}}, }, bson.D{ {"item", "planner"}, {"qty", 75}, {"tags", bson.A{"blank", "red"}}, {"dim_cm", bson.A{22.85, 30}}, }, bson.D{ {"item", "postcard"}, {"qty", 45}, {"tags", bson.A{"blue"}}, {"dim_cm", bson.A{10, 15.25}}, }, } result, err := coll.InsertMany(context.TODO(), docs)
Publisher<Success> insertManyPublisher = collection.insertMany(asList( Document.parse("{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }"), Document.parse("{ item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }"), Document.parse("{ item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }"), Document.parse("{ item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }"), Document.parse("{ item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] }") ));
collection.insertMany(asList( Document.parse("{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }"), Document.parse("{ item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }"), Document.parse("{ item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }"), Document.parse("{ item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }"), Document.parse("{ item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] }") ));
collection.insertMany( listOf( Document("item", "journal") .append("qty", 25) .append("tags", listOf("blank", "red")) .append("dim_cm", listOf(14, 21)), Document("item", "notebook") .append("qty", 50) .append("tags", listOf("red", "blank")) .append("dim_cm", listOf(14, 21)), Document("item", "paper") .append("qty", 100) .append("tags", listOf("red", "blank", "plain")) .append("dim_cm", listOf(14, 21)), Document("item", "planner") .append("qty", 75) .append("tags", listOf("blank", "red")) .append("dim_cm", listOf(22.85, 30)), Document("item", "postcard") .append("qty", 45) .append("tags", listOf("blue")) .append("dim_cm", listOf(10, 15.25)), ) )
await db.inventory.insert_many( [ {"item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [14, 21]}, {"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14, 21]}, { "item": "paper", "qty": 100, "tags": ["red", "blank", "plain"], "dim_cm": [14, 21], }, {"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85, 30]}, {"item": "postcard", "qty": 45, "tags": ["blue"], "dim_cm": [10, 15.25]}, ] )
await db.collection('inventory').insertMany([ { item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [14, 21] }, { item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [14, 21] }, { item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [14, 21] }, { item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [22.85, 30] }, { item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [10, 15.25] } ]);
$db->coll("inventory")->insert_many( [ { item => "journal", qty => 25, tags => [ "blank", "red" ], dim_cm => [ 14, 21 ] }, { item => "notebook", qty => 50, tags => [ "red", "blank" ], dim_cm => [ 14, 21 ] }, { item => "paper", qty => 100, tags => [ "red", "blank", "plain" ], dim_cm => [ 14, 21 ] }, { item => "planner", qty => 75, tags => [ "blank", "red" ], dim_cm => [ 22.85, 30 ] }, { item => "postcard", qty => 45, tags => ["blue"], dim_cm => [ 10, 15.25 ] } ] );
$insertManyResult = $db->inventory->insertMany([ [ 'item' => 'journal', 'qty' => 25, 'tags' => ['blank', 'red'], 'dim_cm' => [14, 21], ], [ 'item' => 'notebook', 'qty' => 50, 'tags' => ['red', 'blank'], 'dim_cm' => [14, 21], ], [ 'item' => 'paper', 'qty' => 100, 'tags' => ['red', 'blank', 'plain'], 'dim_cm' => [14, 21], ], [ 'item' => 'planner', 'qty' => 75, 'tags' => ['blank', 'red'], 'dim_cm' => [22.85, 30], ], [ 'item' => 'postcard', 'qty' => 45, 'tags' => ['blue'], 'dim_cm' => [10, 15.25], ], ]);
db.inventory.insert_many( [ {"item": "journal", "qty": 25, "tags": ["blank", "red"], "dim_cm": [14, 21]}, {"item": "notebook", "qty": 50, "tags": ["red", "blank"], "dim_cm": [14, 21]}, { "item": "paper", "qty": 100, "tags": ["red", "blank", "plain"], "dim_cm": [14, 21], }, {"item": "planner", "qty": 75, "tags": ["blank", "red"], "dim_cm": [22.85, 30]}, {"item": "postcard", "qty": 45, "tags": ["blue"], "dim_cm": [10, 15.25]}, ] )
client[:inventory].insert_many([{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }, { item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }, { item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }, { item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }, { item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] } ])
collection.insertMany(Seq( Document("""{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] }"""), Document("""{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] }"""), Document("""{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] }"""), Document("""{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] }"""), Document("""{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }""") )).execute()
匹配数组
要指定数组的相等条件,请使用查询文档 { <field>: <value> }
,其中 <value>
是要匹配的确切数组,包括元素顺序。
要指定数组的相等条件,请使用查询文档 { <field>: <value> }
,其中 <value>
是要匹配的确切数组,包括元素顺序。
要指定数组的相等条件,请使用查询文档 { <field>: <value> }
,其中 <value>
是要匹配的确切数组,包括元素顺序。
要指定数组的相等条件,可以使用Eq方法,其中<value>
是要匹配的数组的精确值,包括元素的顺序
Builders<BsonDocument>.Filter.Eq(<field>, <value>)
要指定数组的相等条件,使用查询文档eq( <field>, <value>)
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。
要指定数组的相等条件,使用查询文档eq( <field>, <value>)
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。
要指定数组的相等条件,请使用查询文档 { <field>: <value> }
,其中 <value>
是要匹配的确切数组,包括元素顺序。
要指定数组的相等条件,请使用查询文档 { <field>: <value> }
,其中 <value>
是要匹配的确切数组,包括元素顺序。
要指定数组的相等条件,使用查询文档{ <field> => <value> }
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。
要指定数组的相等条件,使用查询文档[ <field> => <value> ]
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。
要指定数组的相等条件,请使用查询文档 { <field>: <value> }
,其中 <value>
是要匹配的确切数组,包括元素顺序。
要指定数组的相等条件,使用查询文档{ <field> => <value> }
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。
要指定数组的相等条件,使用查询文档equal( <field>, <value> )
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。
以下示例查询所有tags
字段的值为正好包含两个元素"red"
和"blank"
,并且顺序正确的文档
db.inventory.find( { tags: ["red", "blank"] } )
将以下过滤器复制到Compass查询栏并点击查找:
{ tags: ["red", "blank"] }

mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "tags", "[", BCON_UTF8 ("red"), BCON_UTF8 ("blank"), "]"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("tags", new[] { "red", "blank" }); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{{"tags", bson.A{"red", "blank"}}}, )
FindPublisher<Document> findPublisher = collection.find(eq("tags", asList("red", "blank")));
FindIterable<Document> findIterable = collection.find(eq("tags", asList("red", "blank")));
val findFlow = collection .find(eq("tags", listOf("red", "blank")))
cursor = db.inventory.find({"tags": ["red", "blank"]})
const cursor = db.collection('inventory').find({ tags: ['red', 'blank'] });
$cursor = $db->coll("inventory")->find( { tags => [ "red", "blank" ] } );
$cursor = $db->inventory->find(['tags' => ['red', 'blank']]);
cursor = db.inventory.find({"tags": ["red", "blank"]})
client[:inventory].find(tags: ['red', 'blank'])
var findObservable = collection.find(equal("tags", Seq("red", "blank")))
如果您想找到包含元素"red"
和"blank"
的数组,而不考虑顺序或其他数组元素,请使用$all
运算符
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
将以下过滤器复制到Compass查询栏并点击查找
{ tags: { $all: ["red", "blank"] } }

mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "tags", "{", "$all", "[", BCON_UTF8 ("red"), BCON_UTF8 ("blank"), "]", "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.All("tags", new[] { "red", "blank" }); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"tags", bson.D{{"$all", bson.A{"red", "blank"}}}}, })
findPublisher = collection.find(all("tags", asList("red", "blank")));
findIterable = collection.find(all("tags", asList("red", "blank")));
val findFlow = collection .find(all("tags", listOf("red", "blank")))
cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
const cursor = db.collection('inventory').find({ tags: { $all: ['red', 'blank'] } });
$cursor = $db->coll("inventory")->find( { tags => { '$all' => [ "red", "blank" ] } } );
$cursor = $db->inventory->find(['tags' => ['$all' => ['red', 'blank']]]);
cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
client[:inventory].find(tags: { '$all' => ['red', 'blank'] })
findObservable = collection.find(all("tags", "red", "blank"))
查询数组中的元素
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { <字段>: <值> }
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { <字段>: <值> }
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用 Eq 方法,其中 <值>
是要匹配的元素值
Builders<BsonDocument>.Filter.Eq(<field>, <value>)
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 eq( <字段>, <值>)
,其中值是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 eq( <字段>, <值>)
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { <字段>: <值> }
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { <字段>: <值> }
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器
{ <字段> => <值> }
,其中值是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 [ <字段> => <值> ]
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { <字段>: <值> }
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { <字段> => <值> }
,其中 <值>
是元素值。
要查询数组字段是否至少包含一个指定值的元素,请使用过滤器 equal( <field>, <value> )
,其中 <value>
是元素值。
以下示例查询所有 tags
是数组且包含字符串 "red"
作为其元素的文档。
db.inventory.find( { tags: "red" } )
将以下过滤器复制到Compass查询栏并点击查找
{ tags: "red" }

mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ("tags", BCON_UTF8 ("red")); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("tags", "red"); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"tags", "red"}, })
findPublisher = collection.find(eq("tags", "red"));
findIterable = collection.find(eq("tags", "red"));
val findFlow = collection .find(eq("tags", "red"))
cursor = db.inventory.find({"tags": "red"})
const cursor = db.collection('inventory').find({ tags: 'red' });
$cursor = $db->coll("inventory")->find( { tags => "red" } );
$cursor = $db->inventory->find(['tags' => 'red']);
cursor = db.inventory.find({"tags": "red"})
client[:inventory].find(tags: 'red')
findObservable = collection.find(equal("tags", "red"))
例如,以下操作查询所有数组 dim_cm
至少包含一个值大于 25
的元素的文档。
db.inventory.find( { dim_cm: { $gt: 25 } } )
将以下过滤器复制到Compass查询栏并点击查找
{ dim_cm: { $gt: 25 } }
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "dim_cm", "{", "$gt", BCON_INT64 (25), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Gt("dim_cm", 25); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"dim_cm", bson.D{ {"$gt", 25}, }}, })
findPublisher = collection.find(gt("dim_cm", 25));
findIterable = collection.find(gt("dim_cm", 25));
val findFlow = collection .find(gt("dim_cm", 25))
cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
const cursor = db.collection('inventory').find({ dim_cm: { $gt: 25 } });
$cursor = $db->coll("inventory")->find( { "dim_cm" => { '$gt' => 25 } } );
$cursor = $db->inventory->find(['dim_cm' => ['$gt' => 25]]);
cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
client[:inventory].find(dim_cm: { '$gt' => 25 })
findObservable = collection.find(gt("dim_cm", 25))
为数组元素指定多个条件
在指定数组元素的复合条件时,可以指定查询,使得单个数组元素满足这些条件,或者任何组合的数组元素满足条件。
在数组元素上使用复合过滤条件查询数组
以下示例查询 dim_cm
数组包含满足查询条件的元素;例如,一个元素可以满足大于 15
的条件,另一个元素可以满足小于 20
的条件,或者单个元素可以同时满足这两个条件。
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
将以下过滤器复制到Compass查询栏并点击查找
{ dim_cm: { $gt: 15, $lt: 20 } }

mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "dim_cm", "{", "$gt", BCON_INT64 (15), "$lt", BCON_INT64 (20), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var builder = Builders<BsonDocument>.Filter; var filter = builder.And(builder.Gt("dim_cm", 15), builder.Lt("dim_cm", 20)); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"dim_cm", bson.D{ {"$gt", 15}, {"$lt", 20}, }}, })
findPublisher = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)));
findIterable = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)));
val findFlow = collection .find(and(gt("dim_cm", 15), lt("dim_cm", 20)))
cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
const cursor = db.collection('inventory').find({ dim_cm: { $gt: 15, $lt: 20 } });
$cursor = $db->coll("inventory")->find( { "dim_cm" => { '$gt' => 15, '$lt' => 20 } } );
$cursor = $db->inventory->find([ 'dim_cm' => [ '$gt' => 15, '$lt' => 20, ], ]);
cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
client[:inventory].find(dim_cm: { '$gt' => 15, '$lt' => 20 })
findObservable = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)))
查询满足多个条件的数组元素
使用 $elemMatch
操作符来指定数组元素上的多个条件,使得至少有一个数组元素满足所有指定的条件。
以下示例查询文档,其中 dim_cm
数组包含至少一个元素,该元素既大于 ($gt
) 22
又小于 ($lt
) 30
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
将以下过滤器复制到Compass查询栏并点击查找
{ dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } }

mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "dim_cm", "{", "$elemMatch", "{", "$gt", BCON_INT64 (22), "$lt", BCON_INT64 (30), "}", "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.ElemMatch<BsonValue>("dim_cm", new BsonDocument { { "$gt", 22 }, { "$lt", 30 } }); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"dim_cm", bson.D{ {"$elemMatch", bson.D{ {"$gt", 22}, {"$lt", 30}, }}, }}, })
findPublisher = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
findIterable = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
val findFlow = collection .find(elemMatch("dim_cm", Document.parse("{ \$gt: 22, \$lt: 30 }")))
cursor = db.inventory.find({"dim_cm": {"$elemMatch": {"$gt": 22, "$lt": 30}}})
const cursor = db.collection('inventory').find({ dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } });
$cursor = $db->coll("inventory")->find( { dim_cm => { '$elemMatch' => { '$gt' => 22, '$lt' => 30 } } } );
$cursor = $db->inventory->find([ 'dim_cm' => [ '$elemMatch' => [ '$gt' => 22, '$lt' => 30, ], ], ]);
cursor = db.inventory.find({"dim_cm": {"$elemMatch": {"$gt": 22, "$lt": 30}}})
client[:inventory].find(dim_cm: { '$elemMatch' => { '$gt' => 22, '$lt' => 30 } })
findObservable = collection.find(elemMatch("dim_cm", Document("$gt" -> 22, "$lt" -> 30)))
按数组索引位置查询元素
使用 点表示法,您可以指定数组特定索引或位置的查询条件。数组使用基于0的索引。
注意
使用点表示法进行查询时,字段和嵌套字段必须放在引号内。
以下示例查询所有数组 dim_cm
的第二个元素大于 25
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
将以下过滤器复制到Compass查询栏并点击查找
{ "dim_cm.1": { $gt: 25 } }

mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "dim_cm.1", "{", "$gt", BCON_INT64 (25), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Gt("dim_cm.1", 25); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"dim_cm.1", bson.D{ {"$gt", 25}, }}, })
findPublisher = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
findIterable = collection.find(gt("dim_cm.1", 25));
val findFlow = collection .find(gt("dim_cm.1", 25))
cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
const cursor = db.collection('inventory').find({ 'dim_cm.1': { $gt: 25 } });
$cursor = $db->coll("inventory")->find( { "dim_cm.1" => { '$gt' => 25 } } );
$cursor = $db->inventory->find(['dim_cm.1' => ['$gt' => 25]]);
cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
client[:inventory].find('dim_cm.1' => { '$gt' => 25 })
findObservable = collection.find(gt("dim_cm.1", 25))
按数组长度查询数组
使用 $size
操作符按元素数量查询数组。例如,以下查询选择数组 tags
有 3 个元素的文档。
db.inventory.find( { "tags": { $size: 3 } } )
将以下过滤器复制到Compass查询栏并点击查找
{ "tags": { $size: 3 } }
mongoc_collection_t *collection; bson_t *filter; mongoc_cursor_t *cursor; collection = mongoc_database_get_collection (db, "inventory"); filter = BCON_NEW ( "tags", "{", "$size", BCON_INT64 (3), "}"); cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
请务必通过调用以下方法清理任何打开的资源,根据需要操作
var filter = Builders<BsonDocument>.Filter.Size("tags", 3); var result = collection.Find(filter).ToList();
cursor, err := coll.Find( context.TODO(), bson.D{ {"tags", bson.D{ {"$size", 3}, }}, })
findPublisher = collection.find(size("tags", 3));
findIterable = collection.find(size("tags", 3));
val findFlow = collection .find(size("tags", 3))
cursor = db.inventory.find({"tags": {"$size": 3}})
const cursor = db.collection('inventory').find({ tags: { $size: 3 } });
$cursor = $db->coll("inventory")->find( { tags => { '$size' => 3 } } );
$cursor = $db->inventory->find(['tags' => ['$size' => 3]]);
cursor = db.inventory.find({"tags": {"$size": 3}})
client[:inventory].find(tags: { '$size' => 3 })
findObservable = collection.find(size("tags", 3))
使用 MongoDB Atlas 查询数组
本节中的示例使用 电影样本数据集。有关如何将样本数据集加载到您的 MongoDB Atlas 部署中的说明,请参阅 加载样本数据。
要在 MongoDB Atlas 中查询数组,请按照以下步骤操作
在 MongoDB Atlas UI 中,转到您的项目的 集群 页面。
如果您尚未显示,请从导航栏的 组织 菜单中选择包含您所需项目的组织。
如果尚未显示,请从导航栏中的项目菜单中选择您的项目。
如果尚未显示,请点击侧边栏中的集群。
集群页面将显示。
指定查询过滤器文档。
要查询包含数组的文档,请指定一个查询过滤器文档。查询过滤器文档使用查询运算符来指定搜索条件。使用以下示例文档在sample_mflix.movies集合中查询数组字段。
要应用查询过滤器,将示例文档复制到过滤器搜索栏中,然后点击应用。
要在数组上指定相等条件,请使用查询文档{ <field>: <value> }
,其中<value>
是要匹配的数组的精确值,包括元素的顺序。以下示例查找具有包含["Action", "Comedy"]
数组的genres
字段的文档
{ genres: ["Action", "Comedy"] }
要找到一个包含元素 Action
和 Comedy
的数组,不考虑顺序或其他数组元素,请使用 $all
操作符
{ genres: { $all: ["Action", "Comedy"] } }
在指定数组元素的复合条件时,可以指定查询,使得单个数组元素满足这些条件,或者任何组合的数组元素满足条件。
在数组元素上使用复合过滤条件查询数组
以下示例查询 cast
数组包含满足查询条件的元素组合的文档。例如,以下过滤器使用 $regex
和 $eq
操作符返回单个数组元素以 Olsen
结尾,另一个元素等于 Mary-Kate Olsen
或满足两个条件的一个单独元素。
{ cast: { $regex: "Olsen$", $eq: "Mary-Kate Olsen" } }
此查询过滤器返回包含 Mary-Kate Olsen
在其演员阵容中的电影,以及包含 Mary-Kate Olsen
和 Ashley Olsen
在其演员阵容中的电影。
查询满足多个标准的数组元素
使用 $elemMatch
操作符来指定数组元素上的多个条件,使得至少有一个数组元素满足所有指定的条件。
以下示例使用$elemMatch
和$ne
操作符来查询文档,其中languages
数组包含至少一个元素,该元素既不是null
也不等于英语
。
{ languages: { $elemMatch: { $ne: null, $ne: "English" } } }
按数组索引位置查询元素
使用 点表示法,您可以指定数组特定索引或位置的查询条件。数组使用基于0的索引。
注意
使用点表示法进行查询时,字段和嵌套字段必须放在引号内。
以下示例使用$ne
操作符来查询所有文档,其中countries
数组的第一个元素不等于USA
{ "countries.0": { $ne: "USA" }
按数组长度查询数组
使用$size
操作符按元素数量查询数组。例如,以下选择具有3个元素的数组genres
的文档。
{ genres: { $size: 3 } }
更多查询教程
有关更多查询示例,请参阅