0% completed
Arrays are a fundamental data type in MongoDB, allowing you to store multiple values in a single field. MongoDB provides powerful querying capabilities to interact with arrays, including querying elements within an array, nested arrays, and using various operators to match specific conditions.
This lesson will cover how to query arrays in MongoDB in detail.
First, we need to insert some documents into the users
collection to work with. These documents will include arrays and nested arrays.
db.users.insertMany([ { name: "Alice", hobbies: ["reading", "traveling", "swimming"], scores: [85, 90, 88], addresses: [ { city: "New York", zip: "10001" }, { city: "Los Angeles", zip: "90001" } ] }, { name: "Bob", hobbies: ["cycling", "hiking"], scores: [78, 82, 79], addresses: [ { city: "Chicago", zip: "60601" } ] }, { name: "Charlie", hobbies: ["gaming", "reading", "traveling"], scores: [92, 85, 90], addresses: [ { city: "Miami", zip: "33101" }, { city: "San Francisco", zip: "94101" } ] } ])
To query documents that contain a specific element in an array, you can use standard equality conditions.
db.collection.find( { <arrayField>: <value> } )
db.users.find({ hobbies: "reading" })
This command retrieves all documents where the hobbies
array contains the element "reading".
db.users.find({ hobbies: { $all: ["reading", "traveling"] } })
This command retrieves all documents where the hobbies
array contains both "reading" and "traveling".
MongoDB provides operators like $elemMatch
to match elements in an array that meet multiple criteria.
db.collection.find( { <arrayField>: { $elemMatch: { <query1>, <query2>, ... } } } )
$elemMatch
to Query Arraysdb.users.find({ scores: { $elemMatch: { $gte: 85, $lt: 90 } } })
This command retrieves all documents where the scores
array contains at least one element that is greater than or equal to 85 and less than 90.
When dealing with arrays of embedded documents, you need to use dot notation to specify the field within the embedded documents.
db.collection.find( { "<arrayField>.<subField>": <value> } )
db.users.find({ "addresses.city": "New York" })
This command retrieves all documents where the addresses
array contains an embedded document with the city
field equal to "New York".
$elemMatch
for Nested Arraysdb.users.find({ addresses: { $elemMatch: { city: "Miami", zip: "33101" } } })
This command retrieves all documents where the addresses
array contains an embedded document with both city
equal to "Miami" and zip
equal to "33101".
You can also query elements in an array by their index.
db.collection.find( { "<arrayField>.<index>": <value> } )
db.users.find({ "scores.0": 85 })
This command retrieves all documents where the first element (index 0) of the scores
array is 85.
MongoDB provides several operators specifically for querying arrays, such as $size
and $slice
.
db.users.find({ hobbies: { $size: 3 } })
This command retrieves all documents where the hobbies
array has exactly 3 elements.
db.users.find( { name: "Alice" }, { scores: { $slice: 2 } } )
This command retrieves the document where name
is "Alice" and returns only the first 2 elements of the scores
array.
.....
.....
.....