SlideShare a Scribd company logo
/*************************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

More Related Content

PPTX
Drupal7 dbtng
PDF
How te bring common UI patterns to ADF
PPTX
How to Bring Common UI Patterns to ADF
TXT
Manage catalog Configueation In Sharepoint PowerShell
PDF
Chaining and function composition with lodash / underscore
PDF
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
PDF
An introduction to property-based testing
PPTX
JQuery Presentation
Drupal7 dbtng
How te bring common UI patterns to ADF
How to Bring Common UI Patterns to ADF
Manage catalog Configueation In Sharepoint PowerShell
Chaining and function composition with lodash / underscore
CakePHPをさらにDRYにする、ドライケーキレシピ akiyan.com 秋田真宏
An introduction to property-based testing
JQuery Presentation

What's hot (20)

PDF
Advanced php testing in action
PDF
Lodash js
PDF
1.4 data cleaning and manipulation in r and excel
PDF
UI components for large amounts of data
PDF
JavaScript Fundamentals with Angular and Lodash
PPT
Moodle Quick Forms
PDF
Internationalizing CakePHP Applications
PPTX
Web весна 2013 лекция 6
PDF
Systemtabledetailsquery
PPTX
Web осень 2012 лекция 6
PDF
The underestimated power of KeyPaths
PDF
Stata cheat sheet: data processing
PDF
Stata cheatsheet transformation
PDF
Stata cheat sheet: data transformation
PDF
Data Preparation- handling missing value
PDF
Functional es6
PDF
Agile database access with CakePHP 3
PDF
Stata Programming Cheat Sheet
PDF
Clojure functions midje
KEY
Dm adapter RubyConf.TW
Advanced php testing in action
Lodash js
1.4 data cleaning and manipulation in r and excel
UI components for large amounts of data
JavaScript Fundamentals with Angular and Lodash
Moodle Quick Forms
Internationalizing CakePHP Applications
Web весна 2013 лекция 6
Systemtabledetailsquery
Web осень 2012 лекция 6
The underestimated power of KeyPaths
Stata cheat sheet: data processing
Stata cheatsheet transformation
Stata cheat sheet: data transformation
Data Preparation- handling missing value
Functional es6
Agile database access with CakePHP 3
Stata Programming Cheat Sheet
Clojure functions midje
Dm adapter RubyConf.TW
Ad

Similar to Array operators (20)

PDF
Data Engineering: Arrays in Mongo DB(and operators)
PDF
WordCamp Portland 2018: PHP for WordPress
PDF
Complete skeletonimport java.util.ArrayList; public class My.pdf
PDF
HELP IN JAVACreate a main method and use these input files to tes.pdf
PPTX
Chapter 2 wbp.pptx
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
PPTX
UNIT IV (4).pptx
PDF
Stata Cheat Sheets (all)
PDF
Everything About PowerShell
PPTX
Drupal 8 database api
PDF
Underscore.js
PDF
In this lab, we will write an application to store a deck of cards i.pdf
PPTX
Comparing 30 MongoDB operations with Oracle SQL statements
PDF
Perl6 Regexen: Reduce the line noise in your code.
PPT
9780538745840 ppt ch06
PDF
Cheat Sheet for Stata v15.00 PDF Complete
PDF
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
PPT
Chapter 08
ODP
Beginning Scala Svcc 2009
Data Engineering: Arrays in Mongo DB(and operators)
WordCamp Portland 2018: PHP for WordPress
Complete skeletonimport java.util.ArrayList; public class My.pdf
HELP IN JAVACreate a main method and use these input files to tes.pdf
Chapter 2 wbp.pptx
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
UNIT IV (4).pptx
Stata Cheat Sheets (all)
Everything About PowerShell
Drupal 8 database api
Underscore.js
In this lab, we will write an application to store a deck of cards i.pdf
Comparing 30 MongoDB operations with Oracle SQL statements
Perl6 Regexen: Reduce the line noise in your code.
9780538745840 ppt ch06
Cheat Sheet for Stata v15.00 PDF Complete
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
Chapter 08
Beginning Scala Svcc 2009
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Understanding_Digital_Forensics_Presentation.pptx
Empathic Computing: Creating Shared Understanding
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Array operators

  • 1. /*************************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" })
  • 2. 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>, ... } } }
  • 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 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
  • 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