CRUD Native Flashcards
What are the 11 most common CRUD methods, what do they return, and are they atomic?
-
insertOne({document}, {options})
Atomic insertMany([{document1}, {document2}], {options})
find({query}, {projections}, {options})
-
findOneAndUpdate({query}, {update}, {options})
returns original document, Atomic -
findOneAndReplace({query}, {replacement}, {options})
returns original document, Atomic -
findOneAndDelete({query}, {options})
returns deleted document, Atomic -
updateOne({query}, {update}, {options})
Atomic updateMany({query}, {update}, {options})
-
replaceOne({query}, {replacement}, {options})
Atomic -
deleteOne({query}, {options})
Atomic deleteMany({query}, {options})
What is the behavior of the following code?
db.inventory.find({status: "Active", quantity: 100})
This is known as a compound query and is equivalent to:
db.inventory.find({$and: [{status: "Active"}, {quantity: 100}]})
This will return documents whose status is “Active” and quantity is 100. The order of the fields specified in the query does not matter!
Write a query that returns the document by selecting “Gianmarco”
{name: {first: "Gianmarco", last: "Barca"}}
db.members.find({"name.first": "Gianmarco"})
Write a query that matches the embedded document
{name: {first: "Gianmarco", last: "Barca"}}
db.members.find({name: {first: "Gianmarco", last: "Barca"}})
Matching embedded documents requires the fields in your query to be in the right order!
What is the difference between compound queries and queries that match embedded documents?
Compound queries do not need to specify the exact order of document fields. This is because under the hood, compound queries use $and
. However, queries that are designed to match embedded documents do need to match all of the fields in the right order. MongoDB does not recommend the use of matching embedded documents
What are the 8 query relational operators?
Give an example of their general syntax
$lt
$gt
$lte
$gte
$eq
$ne
-
$in
Array -
$nin
Array
db.collection.find({field: {$operator: ...}})
What is the behavior of the following code?
db.collection.find({field: {$in: [value1, value2]}})
If field
has any of the values specified in the array, return the document
Write a query that matches the nested document in “Instock”
{"instock: [{ warehouse: "C", qty: 5 }]}
db.collection.find({instock: {warehouse: "C", qty: 5}})
Matching nested documents requires that fields be in the right order!
Write a query that selects the nested document with the “qty” 5
{"instock: [{ warehouse: "C", qty: 5 }]}
db.collection.find({"instock.qty": 5})
What are the 2 most important array query operators and what is their behavior?
What is their general syntax?
-
$all
array -
$elemMatch
object
db.collection.find(field: {$operator: ...})
Given the document:{interests: ["fitness", "programming"]}
Write a query that will return a document that has “fitness” as one of its “interests” array elements
db.members.find({interests: "fitness"})
db.members.find({interests: {$eq: "fitness"}})
Given the document:{tags: ["blank", "black", "red"]}
Write a query that matches “red” and “black” without regard to order
db.collection.find({tags: {$all: ["red", "black"]}})
db.collection.find({tags: {$eq: "red", $eq: "black"}})
What is the difference between the 2 queries?
Given: {scores: [16, 8]}
db.collection.find( { scores: { $gt: 14, $lt: 16 } } )
db.collection.find( { scores: { $elemMatch: { $gt: 14, $lt: 16 } } } )
The first query will return the document since any combination of elements can be used to satisfy the condition. The second equery will not return the document because $elemMatch
only allows a single element to match the condition
Write a query that uses a projection to only return the “last” field
{name: {fist: "Gianmarco", last: "Barca"}}
db.members.find({"name.last": "Barca"}, {"name.last": 1})
What are the 6 most common update operators?
Give an example of their general syntax.
$set
$unset
$rename
$inc
$multiply
$currentDate
db.collection.updateOne({query}, {$operator: {field: value, field2, value2}})
There are more. Please view: https://www.mongodb.com/docs/manual/reference/operator/update-field/