CRUD Native Flashcards

1
Q

What are the 11 most common CRUD methods, what do they return, and are they atomic?

A
  1. insertOne({document}, {options}) Atomic
  2. insertMany([{document1}, {document2}], {options})
  3. find({query}, {projections}, {options})
  4. findOneAndUpdate({query}, {update}, {options}) returns original document, Atomic
  5. findOneAndReplace({query}, {replacement}, {options}) returns original document, Atomic
  6. findOneAndDelete({query}, {options}) returns deleted document, Atomic
  7. updateOne({query}, {update}, {options}) Atomic
  8. updateMany({query}, {update}, {options})
  9. replaceOne({query}, {replacement}, {options}) Atomic
  10. deleteOne({query}, {options}) Atomic
  11. deleteMany({query}, {options})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the behavior of the following code?

db.inventory.find({status: "Active", quantity: 100})
A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a query that returns the document by selecting “Gianmarco”

{name: {first: "Gianmarco", last: "Barca"}}
A
db.members.find({"name.first": "Gianmarco"})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a query that matches the embedded document

{name: {first: "Gianmarco", last: "Barca"}}
A
db.members.find({name: {first: "Gianmarco", last: "Barca"}})

Matching embedded documents requires the fields in your query to be in the right order!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between compound queries and queries that match embedded documents?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 8 query relational operators?
Give an example of their general syntax

A
  1. $lt
  2. $gt
  3. $lte
  4. $gte
  5. $eq
  6. $ne
  7. $in Array
  8. $nin Array

db.collection.find({field: {$operator: ...}})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the behavior of the following code?

db.collection.find({field: {$in: [value1, value2]}})
A

If field has any of the values specified in the array, return the document

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a query that matches the nested document in “Instock”

{"instock: [{ warehouse: "C", qty: 5 }]}
A
db.collection.find({instock: {warehouse: "C", qty: 5}})

Matching nested documents requires that fields be in the right order!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a query that selects the nested document with the “qty” 5

{"instock: [{ warehouse: "C", qty: 5 }]}
A
db.collection.find({"instock.qty": 5})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 2 most important array query operators and what is their behavior?
What is their general syntax?

A
  1. $all array
  2. $elemMatch object
db.collection.find(field: {$operator: ...})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the document:
{interests: ["fitness", "programming"]}
Write a query that will return a document that has “fitness” as one of its “interests” array elements

A
db.members.find({interests: "fitness"})
db.members.find({interests: {$eq: "fitness"}})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Given the document:
{tags: ["blank", "black", "red"]}
Write a query that matches “red” and “black” without regard to order

A
db.collection.find({tags: {$all: ["red", "black"]}})
db.collection.find({tags: {$eq: "red", $eq: "black"}})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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 } } } )
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a query that uses a projection to only return the “last” field

{name: {fist: "Gianmarco", last: "Barca"}}
A
db.members.find({"name.last": "Barca"}, {"name.last": 1})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the 6 most common update operators?
Give an example of their general syntax.

A
  1. $set
  2. $unset
  3. $rename
  4. $inc
  5. $multiply
  6. $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/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Do update operators add a new field if the proposed field does not exist?

A

Yes, as long as the new field does not violate data constraints

17
Q

Could using the $rename operator cause the reordering of fields in the document?

A

Yes. This is why it is important to use this operator with caution

18
Q

When using replace methods, can it replace the original document’s _id value?

A

No. The _id field is immutable

19
Q

What are the 3 most important array update operators and what is their behavior?
What is their general syntax?

A
  1. $push Modifiers: $each, $position, $sort
  2. $pop
  3. $pull
20
Q

Code an example of appending multiple elements to a field array

A
db.collection.updateOne(..., {$push: {fieldArr: {$each: [value1, value2]}}})
21
Q

Does deleting all documents in a collection drop indexes?

A

No. Indexes must be dropped manually after deleting all documents

22
Q

True or False

Inclusion and exclusion statements can be combined in projections

A

False. _id is the only exception

23
Q

Code an example of evaluating the statistical performance of a query

A
db.collection.explain("executionStats").find(...)