Learning MongoDB

0% completed

Previous
Next
Query an Array

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.

Setting Up Example Documents

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" } ] } ])

Querying Elements in an Array

To query documents that contain a specific element in an array, you can use standard equality conditions.

Syntax

db.collection.find( { <arrayField>: <value> } )

Examples

  1. Querying for a Specific Element in an Array
db.users.find({ hobbies: "reading" })

This command retrieves all documents where the hobbies array contains the element "reading".

  1. Querying for Multiple Elements in an Array
db.users.find({ hobbies: { $all: ["reading", "traveling"] } })

This command retrieves all documents where the hobbies array contains both "reading" and "traveling".

Querying Arrays with Conditions

MongoDB provides operators like $elemMatch to match elements in an array that meet multiple criteria.

Syntax

db.collection.find( { <arrayField>: { $elemMatch: { <query1>, <query2>, ... } } } )

Examples

  1. Using $elemMatch to Query Arrays
db.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.

Querying Nested Arrays

When dealing with arrays of embedded documents, you need to use dot notation to specify the field within the embedded documents.

Syntax

db.collection.find( { "<arrayField>.<subField>": <value> } )

Examples

  1. Querying for a Specific Field in Nested Arrays
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".

  1. Using $elemMatch for Nested Arrays
db.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".

Querying Arrays by Index

You can also query elements in an array by their index.

Syntax

db.collection.find( { "<arrayField>.<index>": <value> } )

Examples

  1. Querying by Index
db.users.find({ "scores.0": 85 })

This command retrieves all documents where the first element (index 0) of the scores array is 85.

Using Array Query Operators

MongoDB provides several operators specifically for querying arrays, such as $size and $slice.

Examples

  1. Querying Arrays by Size
db.users.find({ hobbies: { $size: 3 } })

This command retrieves all documents where the hobbies array has exactly 3 elements.

  1. Projecting a Subset of an Array
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.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next