➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.
This page provides examples of query operations on array fields using the
db.collection.find()
method in mongosh
.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using MongoDB Compass.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using mongoc_collection_find_with_opts.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the MongoCollection.Find() method in the MongoDB C# Driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the Collection.Find function in the MongoDB Go Driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the com.mongodb.client.MongoCollection.find method in the MongoDB com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields by using the MongoCollection.find() method in the MongoDB com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the
motor.motor_asyncio.AsyncIOMotorCollection.find
method in the Motor
driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the Collection.find() method in the MongoDB Node.js Driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the
MongoDB\\Collection::find()
method in the
MongoDB PHP Library.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the
pymongo.collection.Collection.find
method in the
PyMongo
Python driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the Mongo::Collection#find() method in the MongoDB Ruby Driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
This page provides examples of query operations on array fields using the collection.find() method in the MongoDB Scala Driver.
The examples on this page use the inventory
collection. Connect to a
test database in your MongoDB instance then create the inventory
collection:
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 ] } ]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
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] } ]);
$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()
Match an Array
To specify equality condition on an array, use the query
document { <field>: <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document { <field>: <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document { <field>: <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, construct a filter
using the Eq method,
where <value>
is the exact array to match, including the
order of the elements:
Builders<BsonDocument>.Filter.Eq(<field>, <value>)
To specify equality condition on an array, use the query
document eq( <field>, <value>)
where <value>
is
the exact array to match, including the order of the
elements.
To specify equality condition on an array, use the query
document eq( <field>, <value>)
where <value>
is
the exact array to match, including the order of the
elements.
To specify equality condition on an array, use the query
document { <field>: <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document { <field>: <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document [ <field> => <value> ]
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document { <field>: <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document { <field> => <value> }
where <value>
is the
exact array to match, including the order of the elements.
To specify equality condition on an array, use the query
document equal( <field>, <value> )
where <value>
is
the exact array to match, including the order of the
elements.
The following example queries for all documents where the field tags
value is an array with exactly two elements, "red"
and "blank"
,
in the specified order:
db.inventory.find( { tags: ["red", "blank"] } )
Copy the following filter into the Compass query bar and click Find:
{ 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->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")))
If, instead, you wish to find an array that contains both the elements
"red"
and "blank"
, without regard to order or other elements in
the array, use the $all
operator:
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
Copy the following filter into the Compass query bar and click Find:
{ 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->inventory->find(['tags' => ['$all' => ['red', 'blank']]]);