/*************************Query Operator Array**************************/
Name Description
$all Matches arrays that contain all elements specified in
the query.
$elemMatch Selects documents if element in the array field matches
all the specified
$elemMatch conditions.
$size Selects documents if the array field is a specified
size.
$all
The $all operator selects the documents where the value of a field is an array
that contains all the
specified elements. To specify an $all expression, use the following prototype:
{ <field>: { $all: [ <value1> , <value2> ... ] } }
db.inventory.insert([
{
_id: 1,
code: "xyz",
tags: [ "school", "book", "bag", "headphone", "appliance" ],
qty: [
{ size: "S", num: 10, color: "blue" },
{ size: "M", num: 45, color: "blue" },
{ size: "L", num: 100, color: "green" }
]
},
{
_id: 2,
code: "abc",
tags: [ "appliance", "school", "book" ],
qty: [
{ size: "6", num: 100, color: "green" },
{ size: "6", num: 50, color: "blue" },
{ size: "8", num: 100, color: "brown" }
]
},
{
_id: 3,
code: "efg",
tags: [ "school", "book" ],
qty: [
{ size: "S", num: 10, color: "blue" },
{ size: "M", num: 100, color: "blue" },
{ size: "L", num: 100, color: "green" }
]
},
{
_id: 4,
code: "ijk",
tags: [ "electronics", "school" ],
qty: [
{ size: "M", num: 100, color: "green" }
]
}]
);
db.inventory.find({ "tags.0": "school" })
Use $all to Match Values
________________________________
The following operation uses the $all operator to query the inventory collection
for documents where
the value of the tags field is an array whose elements include appliance,
school, and book:
db.collection.find({},{})
db.inventory.find( { tags: { $all: [ "appliance", "school", "book" ] } } )
$gt
$or
$inc
$lte
$toUpper
Use $all with $elemMatch
________________________________
If the field contains an array of documents, you can use the $all with the
$elemMatch operator.
The below operation queries the inventory collection for documents where the
value of the
qty field is an array whose elements match the $elemMatch criteria:
db.inventory.find( {
qty: { $all: [
{ "$elemMatch" : { size: "M", num: { $lt:
50} } },
{ "$elemMatch" : { num : 100, color: "green"
} }
] }
} )
db.inventory.find( {
qty: { $all: [
{ "$elemMatch" : { size: "M", num: { $gt: 50} } },
{ "$elemMatch" : { num : 100, color: "green" } }
] }
} )
db.inventory.find( { "qty.num": { $all: [ 50,100 ] } } )
db.inventory.find( { "qty.num" : 50 } )
$elemMatch (query)
_________________________________
The $elemMatch operator matches documents that contain an array field with at
least one element that matches all the specified query criteria.
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
db.score.insert([
{ _id: 1, results: [ 82, 85, 88 ] },
{ _id: 2, results: [ 75, 88, 89 ] }
])
db.score.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)
Array of Embedded Documents
___________________________
db.survey.insert([
{ _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 }
] },
{ _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score:
7 } ] },
{ _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score:
8 } ] }
])
db.survey.find(
{ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)
db.survey.find(
{ "results.product": "xyz" }
)
/***************************Operators ***********************************/
$in
The $in operator selects the documents where the value of a field equals any
value in the specified array.
To specify an $in expression, use the following prototype:
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
db.employee.find( { employee_id: { $in: [ 50002, 50004 ] } } )
/************************************************************************/
db.inventory.insert([
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] },
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] },
{ _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] },
{ _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] },
{ _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ],
"C" ] }
])
Equals a Specified Value
________________________
1. db.inventory.find( { qty: { $eq: 20 } } ).pretty()
Field in Embedded Document Equals a Value
__________________________________________
2. db.inventory.find( { "item.name": { $eq: "ab" } } ).pretty()
Array Element Equals a Value
____________________________
/*********** Issue *************************/
3. db.inventory.find( { tags: { $eq: "B" } } )
Equals an Array Value
_____________________
db.inventory.find( { tags: { $eq: [ "A", "B" ] } } )
/**************************Array Update Operators
*********************************/
1. Update Operators
2. Update Operator Modifiers
3. Update Operators
Name Description
$ Acts as a placeholder to update the first element that
matches the query condition in an update.
$addToSet Adds elements to an array only if they do not already exist in
the set.
$pop Removes the first or last item of an array.
$pullAll Removes all matching values from an array.
$pull Removes all array elements that match a specified query.
$pushAll Deprecated. Adds several items to an array.
$push Adds an item to an array.
Update Operator Modifiers
Name Description
$each Modifies the $push and $addToSet operators to append multiple
items for array updates.
$slice Modifies the $push operator to limit the size of updated
arrays.
$sort Modifies the $push operator to reorder documents stored in an
array.
$position Modifies the $push operator to specify the position in the
array to add elements.
$ (update)
_________________
Update Values in an Array
_________________________
db.student.insert(
[
{ "_id" : 1, "grades" : [ 80, 85, 90 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
]
)
To update 80 to 82 in the grades array in the first document,
use the positional $ operator if you do not know the position of the element in
the array:
db.student.update(
{ _id: 1, "grades": 80 },
{ $set: { "grades.1" : 82 } }
)
Note: Remember that the positional $ operator acts as a placeholder for the
first match of the update query
the array:
db.student.update(
{ _id: 1, "grades": 80 },
{ $set: { "grades.1" : 82 } }
)
Note: Remember that the positional $ operator acts as a placeholder for the
first match of the update query

Array operators

  • 1.
    /*************************Query Operator Array**************************/ NameDescription $all Matches arrays that contain all elements specified in the query. $elemMatch Selects documents if element in the array field matches all the specified $elemMatch conditions. $size Selects documents if the array field is a specified size. $all The $all operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an $all expression, use the following prototype: { <field>: { $all: [ <value1> , <value2> ... ] } } db.inventory.insert([ { _id: 1, code: "xyz", tags: [ "school", "book", "bag", "headphone", "appliance" ], qty: [ { size: "S", num: 10, color: "blue" }, { size: "M", num: 45, color: "blue" }, { size: "L", num: 100, color: "green" } ] }, { _id: 2, code: "abc", tags: [ "appliance", "school", "book" ], qty: [ { size: "6", num: 100, color: "green" }, { size: "6", num: 50, color: "blue" }, { size: "8", num: 100, color: "brown" } ] }, { _id: 3, code: "efg", tags: [ "school", "book" ], qty: [ { size: "S", num: 10, color: "blue" }, { size: "M", num: 100, color: "blue" }, { size: "L", num: 100, color: "green" } ] }, { _id: 4, code: "ijk", tags: [ "electronics", "school" ], qty: [ { size: "M", num: 100, color: "green" } ] }] ); db.inventory.find({ "tags.0": "school" })
  • 2.
    Use $all toMatch Values ________________________________ The following operation uses the $all operator to query the inventory collection for documents where the value of the tags field is an array whose elements include appliance, school, and book: db.collection.find({},{}) db.inventory.find( { tags: { $all: [ "appliance", "school", "book" ] } } ) $gt $or $inc $lte $toUpper Use $all with $elemMatch ________________________________ If the field contains an array of documents, you can use the $all with the $elemMatch operator. The below operation queries the inventory collection for documents where the value of the qty field is an array whose elements match the $elemMatch criteria: db.inventory.find( { qty: { $all: [ { "$elemMatch" : { size: "M", num: { $lt: 50} } }, { "$elemMatch" : { num : 100, color: "green" } } ] } } ) db.inventory.find( { qty: { $all: [ { "$elemMatch" : { size: "M", num: { $gt: 50} } }, { "$elemMatch" : { num : 100, color: "green" } } ] } } ) db.inventory.find( { "qty.num": { $all: [ 50,100 ] } } ) db.inventory.find( { "qty.num" : 50 } ) $elemMatch (query) _________________________________ The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. { <field>: { $elemMatch: { <query1>, <query2>, ... } } }
  • 3.
    db.score.insert([ { _id: 1,results: [ 82, 85, 88 ] }, { _id: 2, results: [ 75, 88, 89 ] } ]) db.score.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } } ) Array of Embedded Documents ___________________________ db.survey.insert([ { _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] }, { _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] }, { _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] } ]) db.survey.find( { results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } } ) db.survey.find( { "results.product": "xyz" } ) /***************************Operators ***********************************/ $in The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype: { field: { $in: [<value1>, <value2>, ... <valueN> ] } } db.employee.find( { employee_id: { $in: [ 50002, 50004 ] } } ) /************************************************************************/ db.inventory.insert([ { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] }, { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] }, { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] }, { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] }, { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] } ]) Equals a Specified Value ________________________ 1. db.inventory.find( { qty: { $eq: 20 } } ).pretty()
  • 4.
    Field in EmbeddedDocument Equals a Value __________________________________________ 2. db.inventory.find( { "item.name": { $eq: "ab" } } ).pretty() Array Element Equals a Value ____________________________ /*********** Issue *************************/ 3. db.inventory.find( { tags: { $eq: "B" } } ) Equals an Array Value _____________________ db.inventory.find( { tags: { $eq: [ "A", "B" ] } } ) /**************************Array Update Operators *********************************/ 1. Update Operators 2. Update Operator Modifiers 3. Update Operators Name Description $ Acts as a placeholder to update the first element that matches the query condition in an update. $addToSet Adds elements to an array only if they do not already exist in the set. $pop Removes the first or last item of an array. $pullAll Removes all matching values from an array. $pull Removes all array elements that match a specified query. $pushAll Deprecated. Adds several items to an array. $push Adds an item to an array. Update Operator Modifiers Name Description $each Modifies the $push and $addToSet operators to append multiple items for array updates. $slice Modifies the $push operator to limit the size of updated arrays. $sort Modifies the $push operator to reorder documents stored in an array. $position Modifies the $push operator to specify the position in the array to add elements. $ (update) _________________ Update Values in an Array _________________________ db.student.insert( [ { "_id" : 1, "grades" : [ 80, 85, 90 ] }, { "_id" : 2, "grades" : [ 88, 90, 92 ] }, { "_id" : 3, "grades" : [ 85, 100, 90 ] } ] ) To update 80 to 82 in the grades array in the first document, use the positional $ operator if you do not know the position of the element in
  • 5.
    the array: db.student.update( { _id:1, "grades": 80 }, { $set: { "grades.1" : 82 } } ) Note: Remember that the positional $ operator acts as a placeholder for the first match of the update query
  • 6.
    the array: db.student.update( { _id:1, "grades": 80 }, { $set: { "grades.1" : 82 } } ) Note: Remember that the positional $ operator acts as a placeholder for the first match of the update query