Array Operations
Estimated time to read: 3 minutes
Overview¶
$push
allows adding an element to an array
$push
can also turn a field into an array field if it was previously a different type
$all
returns the documents where the value of a field is an array that contains all the specified elements, regardless of their order. Its syntax is {<array field> : {"$all" : <array>}}
$size
matches any array with the number of elements specified by the argument. Its syntax is {<array field> : {"$size" : <number>}}
Usage / Example¶
In the example below, amenities
is an array field that stores multiple results.
Using the sample_airbnb database, querying {"amenities": "Shampoo"}
will return any listings that have Shampoo as an entry within the amenities array. However, it is not obvious that amenities is an array field.
Note
{"amenities": ["Shampoo"}]
will not return any results, as no document within the collection has an array that only consists of the Shampoo entry.
Note
The order of elements within an array field query does matter!
To return documents that have the required amenities, in any order, the $all
array operator can be used.
db.listingsAndReviews.find({ "amenities": {
"$size": 20,
"$all": [ "Internet", "Wifi", "Kitchen",
"Heating", "Family/kid friendly",
"Washer", "Dryer", "Essentials",
"Shampoo", "Hangers",
"Hair dryer", "Iron",
"Laptop friendly workspace" ]
}
}).pretty()
Projection¶
A projection can be used to return only the requested fields from a document for review. By default, Projection will return the value of the _id
field.
Projection Syntax¶
The syntax for projection is:
db.<collection>.find( { <query> }, { <projection> } )
The above syntax specifies which fields shouild or should not be included in the result cursor. 1
and 0
can be used to include or negate a field in a projection.
Note
1
and 0
can not be mixed in the projection statement!
Projection Example¶
For example:
To find all documents with exactly 20 amenities which include all the amenities listed in the query array, and display their price and address:
db.listingsAndReviews.find({ "amenities":
{ "$size": 20, "$all": [ "Internet", "Wifi", "Kitchen", "Heating",
"Family/kid friendly", "Washer", "Dryer",
"Essentials", "Shampoo", "Hangers",
"Hair dryer", "Iron",
"Laptop friendly workspace" ] } },
{"price": 1, "address": 1}).pretty() // Note that additional statement here to request a projection of fields `price` and `address`.
To find all documents that have Wifi as one of the amenities only include price and address in the resulting cursor:
db.listingsAndReviews.find({ "amenities": "Wifi" },
{ "price": 1, "address": 1, "_id": 0 }).pretty()
To find all documents that have Wifi as one of the amenities only include price and address in the resulting cursor, also exclude "maximum_nights"
. *This will be an error:
db.listingsAndReviews.find({ "amenities": "Wifi" },
{ "price": 1, "address": 1,
"_id": 0, "maximum_nights":0 }).pretty()
Element Matching¶
To access elements within the sub-document of an array field, the $elemMatch
operator can be used.
Element Matching Syntax¶
`{
The above syntax matches documents that contain an array field with at least one element that matches the specified query criteria.
Element Matching Example¶
To find all documents where the student in class 431 received a grade higher than 85 for any type of assignment:
db.grades.find({ "class_id": 431 },
{ "scores": { "$elemMatch": { "score": { "$gt": 85 } } }
}).pretty()
To find all documents where the student had an extra credit score: