2. Array in MongoDB
• Array is a list of values that can take many different
forms.
• Create a document:
db.Collection_name.operation({field: [“Val1”,“Val2”]})
3. Querying on Array
• The find method can be used to return arrays inside a document.
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.
Example:
Query 1: db.CourseRepository.find( { tags: [“blue", “red"] } )
Query 2: db.CourseRepository.find( { tags: { $all: [“blue", “red"] } } )
4. • This query would return the document having
tags “blue" and ”red", in the specified order.
• To find an array that contains both the
elements “blue" and ”red", without regard to
order or other elements in the array, use
the $all operator:
7. Query an Array for an Element:
• To query if the array field contains at least one element with the
specified value, use the filter { <field>: <value> } .
Example: db.CourseRepository.find( { tags: "red" } )
• To specify conditions on the elements in the array field, use query
operators in the query filter document:
Syntax: { <array field>: { <operator1>: <value1>, ... } }
Example: db.CourseRepository.find( { dim_cm: { $gt: 25 } } )
• This operation queries for all documents where the
array dim_cm contains at least one element whose value is greater
than 25.
8. • When specifying compound conditions on array elements, the
query can be specified such that either a single array element
meets these condition or any combination of array elements
meets the conditions.
Query an Array with Compound Filter Conditions on the Array
Elements: db.CourseRepository.find( { dim_cm: { $gt: 15, $lt: 20 } } )
• This example queries for documents where the dim_cm array
contains elements that in some combination satisfy the query
conditions; e.g., one element can satisfy the greater
than 15 condition and another element can satisfy the less
than 20 condition, or a single element can satisfy both.
Specify Multiple Conditions for Array Elements
10. Query for an Array Element that Meets Multiple Criteria:
• Use $elemMatch operator to specify multiple criteria on the elements of
an array such that at least one array element satisfies all the specified
criteria.
db.CourseRepository.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
• This example queries for documents where the dim_cm array contains at
least one element that is both greater than ($gt) 22 and less than ($lt) 30:
Query for an Element by the Array Index Position
• Using the dot notation, you can specify query conditions for an element at
a particular index or position of the array. The array uses zero-based
indexing.
db. CourseRepository.find( { "dim_cm.1": { $gt: 25 } } )
• This example queries for all documents where the second element in the
array dim_cm is greater than 25.
11. Query an Array by Array Length
• Use the $size operator to query for arrays by number of elements.
db.CourseRepository.find( { "tags": { $size: 3 } } )
• This query selects documents where array tags has 3 elements.
Nested array in MongoDB
when a set of field values is bundled inside a particular field in the
form of an array, it is said to be “embedded” as a nested Array
document.
13. Array operator: $pop
• The $pop operator removes the first or last element of an
array.
• Pass $pop a value of -1 to remove the first element of an
array and 1 to remove the last element in an array.
• The $pop operator has the form:
{ $pop: { <field>: <-1 | 1>, ... } }
• The $pop operation fails if the <field> is not an array.
• If the $pop operator removes the last item in the <field>,
the <field> will then hold an empty array.
14. Examples
Remove the First Item of an Array:
Create the students collection:
Query: Remove first element, 8, from the scores array:
Output: The first element, 8, has been removed from
the scores array:
Remove the Last Item of an Array
Remove the last element, 10, from the scores array by specifying 1:
The last element, 10, has been removed from the scores array:
15. Array operator: $pull
• The $pull operator removes from an existing array all instances of a value
or values that match a specified condition.
• Syntax:
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
To specify a <field> in an embedded document or in an array, use dot
notation.
• If you specify a <condition> and the array elements are embedded
documents, $pull operator applies the <condition> as if each array
element were a document in a collection.
• If the specified <value> to remove is a document, $pull removes only the
elements in the array that have the exact same fields and values. The
ordering of the fields can differ.
16. Array operator: $pull Examples
Remove All Items That Equal a Specified Value
1) Create the stores collection: 3) Output:
2) Query:
This operation removes
(i) "apples" and "oranges" from the fruits array
(ii) "carrots" from the vegetables array
17. Remove All Items That Match a Specified $pull Condition
Create the profiles collection:
Remove all items from the votes array that are greater than or equal to ( $gte ) 6:
After the update operation, the document only has values less than 6:
Remove Items from an Array of Documents
Create the survey collection:
Removes all elements from the results array that contain both a score field equal
to 8 and an item field equal to "B":
18. • The $pull expression applies the condition to each element of
the results array as though it were a top-level document.
• After the operation, the results array contains no documents that
contain both a score field equal to 8 and an item field equal to "B".
• Create the survey collection:
• Removes all elements from the results array that contain both a score field
equal to 8 and an item field equal to "B":
• Query:
• Result:
19. Remove Documents from Nested Arrays
Create a new survey collection with documents that are embedded in nested arrays.
Specify multiple conditions on the elements of the answers array with $elemMatch:
21. Array operator: $pullall
• The $pullAll operator removes all instances of the specified values from an
existing array.
• Unlike the $pull operator that removes elements by specifying a
query, $pullAll removes elements that match the listed values.
• Syntax: { $pullAll: { <field1>: [ <value1>, <value2> ... ], .. } }
To specify a <field> in an embedded document or in an array, use dot notation.
Example: Create the survey collection:
Remove all instances of the values "0" and "5" from the scores array:
Output:
22. Array Update Operator: $push
• The $push operator appends a specified value to an array.
Syntax: { $push: { <field1>: <value1>, ... } }
To specify a <field> in an embedded document or in an array, use dot notation.
• If the field is absent in the document to update, $push adds the
array field with the value as its element.
• If the field is not an array, the operation will fail.
• If the value is an array, $push appends the whole array as
a single element. To add each element of the value separately,
use the $each modifier with $push.
23. Create the students collection:
The following example appends 89 to the scores array:
Output:
$push operator: Append a Value to an
Array
24. $push operator: Append a Value to Arrays in
Multiple Documents
• Add the following documents to the students collection:
• The following $push operation appends 95 to the scores array in
each document:
• To confirm that each scores array includes 95, run the following
operation:
• Output
25. $push operator: Append Multiple Values to an Array
• Use $push with the $each modifier to append multiple values to
the array field.
• The following example appends each element of [ 90, 92, 85 ] to
the scores array for the document where the name field
equals joe:
26. Array operator: $addToSet
• The $addToSet operator adds a value to an array unless the
value is already present, in which case $addToSet does nothing
to that array.
• Syntax:
{ $addToSet: { <field1>: <value1>, ... } }
• To specify a <field> in an embedded document or in an array,
use dot notation.
• $addToSet only ensures that there are no duplicate
items added to the set and does not affect existing duplicate
elements.
• $addToSet does not guarantee a particular ordering of elements
in the modified set.
27. Missing Field:
• If $addToSet used on a field that is absent from the
document to update, $addToSet creates the array field
with the specified value as its element.
Field is Not an Array:
• If you use $addToSet on a field that is not an array, the
operation will fail.
• For example, create the pigments collection:
• The colors field is not an array. The
following $addToSet operation fails:
28. • If the value is an array, $addToSet appends the whole array
as a single element.
Create the alphabet collection:
• The following operation appends the array [ "c", "d" ] to
the letters field:
• The array [ "c", "d" ] is added as a single element:
• To add each element of the value separately, use
the $each modifier with $addToSet.
$addToSet: Value to Add is An Array
29. $addToSet: Value to Add is a Document
• If the value is a document, MongoDB determines that the
document is a duplicate if an existing document in the array
matches the to-be-added document exactly; i.e. the existing
document has the exact same fields and values and the fields are in
the same order.
• Add to Array: Add the element "accessories" to the tags array
since "accessories" does not exist in the array.
• Value Already Exists: $addToSet operation has no effect
because "camera" is already an element of the tags array: