1/47
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
Inserting Document
These are the basic methods for adding data to MongoDB.
insertOne
To insert a single document, use the collection’s method.
_id
insertOne will automatically add an "____" key to the document if you do not provide one.
insertMany
If you need to insert multiple documents into a collection, you can use _.
ordered or unordered
When performing a bulk insert using insertMany, if a document halfway through the array produces an error of some type, what happens depends on whether you have opted for or __ operations.
Ordered inserts
This insert is the default if no order is specified.
Insertion
For ordered inserts, the array passed to insertMany defines the _ order. If a document produces an insertion error, no documents beyond that point in the array will be inserted.
Unordered inserts
In this insert, MongoDB will attempt to insert all documents, regardless of whether some insertions produce errors.
deleteOne and deleteMany
Removing Documents: MongoDB provides _ and _ for this purpose.
deleteOne
To delete the document with the "id" value of 4, we use ___ in the mongo shell.
deleteMany
To delete all the documents that match a filter, use __:
updateOne, updateMany, and replaceOne
Updating Documents: Once a document is stored in the database, it can be changed using one of several update methods: , , and .
updateOne and updateMany
each take a filter document as their first parameter and a modifier document, which describes changes to make, as the second parameter.
first parameter, second parameter
updateOne and updateMany each take a filter document as their ___ and a modifier document, which describes changes to make, as the ______ .
replaceOne
It fully replaces a matching document with a new one. This can be useful to do a dramatic schema migration. Example: suppose we are making major changes to a user document, which we want to move the "friends" and "enemies" fields to a "relationships" subdocument. We can change the structure of the document in the shell and then replace the database’s version with a replaceOne.
$set
This operator sets the value of a field. If the field does not yet exist, it will be created. $set
$set
This operator can even change the type of the key it modifies. For example, we want to add another value of books that the user likes; this will change the type of "favorite book" key into an array.
$unset
If we want to remove the “favorite book” key altogether, we can use the operator.
$set
We can also use ____ operator to reach in and change embedded documents.
updateMany
To modify all of the documents matching a filter, use. It follows the same semantics as updateOne and takes the same parameters. The key difference is in the number of documents that might be changed.
find()
Querying Documents: The method is used to perform queries in MongoDB. Querying returns a subset of documents in a collection.
first argument
Which documents get returned is determined by the ___ to find, which is a document specifying the query criteria.
empty query document
(i.e., db.movies.find()) will match everything in the “movies” collection. We begin restricting our search when we start adding key/value pairs to the query document.
Multiple conditions
It can be strung together by adding more key/value pairs to the query document, which gets interpreted as “condition1 AND condition2" > db.users.find({"username" : "John", "age" : 24})
$lt, $lte, $gt, and $gte
Query conditional: ___, ____, ____, and ____are all comparison operators, corresponding to
$ne
To query for documents where a key’s value is not equal to a certain value, you can use conditional operator, ____, which stands for “not equal.
$in
This operator can be used to query for a variety of values for a single key.
$or
This operator is more general; it can be used to query for any of the given values across multiple keys.
$not
This operator is useful to find all documents that don’t match a given pattern.
$regex
provides regular expression capabilities for pattern matching strings in queries. Regular expressions
Regular expression flags
(e.g., i) are allowed but not required. If we want to match not only various capitalizations of “john,” but also “johnny,” we can continue to improve our regular expression by doing this: > db.users.find({"name" : /johnny?/i})
Array
Querying for elements of an _ is designed to behave the way querying for scalars (string, numbers, date, etc.) does. For example, if the array is a list of fruits, like this: > db.food.insertOne({"fruit" : ["apple", "banana", "peach"] })
$all
If you need to match arrays by more than one element, you can use ____. This allows you to match a list of elements.
key.index
If we want to query for a specific element of an array, you can specify an index using the syntax _. > db.food.find({"fruit.2" : "peach"})
$size
matches any array with the number of elements specified by the argument. Here’s an example: > db.food.find({"fruit" : {"$size" : 3}})
$slice
an operator that can be used to return a subset of elements for an array key
first 10 comments
For example, suppose we had a blog post document and we wanted to return the __ ___: > db.blog.posts.findOne(criteria, {"comments" : {"$slice" : 10}})
last 10 comments
Alternatively, if we wanted the _ ___ , we could use −10: > db.blog.posts.findOne(criteria, {"comments" : {"$slice" : -10}})
24th
It can also return pages in the middle of the results by taking an offset and the number of elements to return: > db.blog.posts.findOne(criteria, {"comments" : {"$slice" : [23, 10]}}) 24th through 33rd
Embedded documents or nested documents
These are types of documents which contain a document inside another document.
dot notation
You can query for embedded keys using ___ : > db.people.find({"name.first" : "John", "name.last" : "Escalona"})
Data Modelling
In MongoDB, data has a flexible schema. It is totally different from the SQL database, where you had to determine and declare a table's schema before inserting data. MongoDB collections do not enforce document structure. The main challenge in data modeling is balancing the need of the application, the performance characteristics of the database engine, and the data retrieval patterns.
document structure
MongoDB collections do not enforce . The main challenge in data modeling is balancing the need of the application, the performance characteristics of the database engine, and the data retrieval patterns.
Embedded Data
Embedded documents capture relationships between data by storing related data in a single document structure. MongoDB documents make it possible to embed document structures in a field or array within a document. These denormalized data models allow applications to retrieve and manipulate related data in a single database operation.
single document structure
Embedded documents capture relationships between data by storing related data in a _ ___.
Denormalized
MongoDB documents make it possible to embed document structures in a field or array within a document. These __ data models allow applications to retrieve and manipulate related data in a single database operation.
References
These store the relationships between data by including links or references from one document to another. Applications can resolve these references to access the related data. Broadly, these are normalized data models.
Normalized
Applications can resolve these references to access the related data. Broadly, these are __ data models.