MongoDB M001
ObjectId()
It is the default value for the "_id" field unless otherwise specified.
Query Operator
Query operators provide additional ways to locate data within the database. $ has multiple uses Precedes MQL operators Precedes Aggregation pipeline stages Allow access to field values.
$set operator
Set field value to a new specified value. Usage: {"$set":{"pop":17630,"<field2>":<new value>, ...}}
How to see the databases in mongo shell?
show dbs
What is a collection?
A collection is an organized store of documents in MongoDB, usually with common fields between documents.
Replica set in Atlas
A few connected MongoDB instances that store the same data.
updateOne()
If there are multiple documents that match a given criteria, only one of them will be updated whichever one this operation finds first.
What is the purpose of the $ symbol ?
$ denotes the use of an operator $ can be used to access the field value
Data Modeling with MongoDB
1) Data that is used together should be stored together. 2) Evolving application implies an evolving and evolving data model.
What is cursor?
A cursor is a pointer to a result set of a query. A pointer is a direct address of the memory location.
What is a document?
A document is a way to organize data as a set of field-value pairs.
Clusters in Atlas
A group of servers that stores your data.
What is a mongoDB?
A mongoDb is a no-sql document database.
Quiz: Insert Errors Problem: Select all true statements from the following list: A) MongoDB can always store duplicate documents in the same collection regardless of the _id value. B) If a document is inserted without a provided _id value, then the _id field and value will be automatically generated for the inserted document before insertion. C) If a document is inserted without a provided _id value, then that document will fail to be inserted and cause a write error. D) MongoDB can store duplicate documents in the same collection, as long as their _id values are different. E) There is no way to ensure that duplicate records are not stored in MongoDB.
Ans) B, D
Data Explorer
Atlas UI provides us with Data Explorer so that we can query data using the GUI. Query must use vaid JSON. Returned documents will contain the requested field:value pairs in them.
Single Cluster in Atlas
Automatically configured as a replica set.
Why is mongoDb a NoSQL database?
Because it uses a structured way to store and access data. Because it does not utilize tables, rows and columns to organize data.
If upsert is true
By default,upsert is set to false. But if you set it to true then you can expect it to either do an update or an insert. The update will happen if there are documents that match the filter criteria of the update operation. The insert will happen if there are no documents this criteria.
What is a document? In MongoDB how does a document relate to a collection?
Collections consist of one or many documents
What is a Document? In a MongoDB Document what is the role of fields and values?
Each field has a value associated with it. A field is a unique identifier for a specific datapoint.
$inc operator
Increment field value by a specified amount. Usage: {"$inc": {"pop":10,"<field2>":<increment value><...}}}
find()
It return a cursor with all the documents that correspond to the given query.
How to delete a collection?
To delete a collection you can use a drop function. >>db.<collection>.drop()
When you need to include the same operator more than once in a query?
Using the routes collection find out how many CR2 and A81 airplanes come through the KZN airport? {"$or" :[{dst_airport: "KZN"}, {src_airport:"KZN"}]} and {"$or":[{airplane:"CR2"}, {airplane:"A81"}]}
Get one document from the collection:
db.grades.findOne()
Find all documents where airplanes CR2 or A81 left or landed in the KZN airport:
db.routes.find({ "$and": [ { "$or" :[ { "dst_airport": "KZN" }, { "src_airport": "KZN" } ] }, { "$or" :[ { "airplane": "CR2" }, { "airplane": "A81" } ] } ]}).pretty()
Find all documents where the trip lasted longer than 1200 seconds, and started and ended at the same station:
db.trips.find({ "$expr": { "$and": [ { "$gt": [ "$tripduration", 1200 ]}, { "$eq": [ "$end station id", "$start station id" ]} ]}}).count()
what is it command?
it -- iterates through a cursor
How to view BSON or JSON file in terminal?
less xyz.bson less xyz.json
Export data in BSON
mongodump --uri "<Atlas Cluster URI>"
Index Query Example
use sample_training db.trips.find({ "birth year": 1989 }) db.trips.find({ "start station id": 476 }).sort( { "birth year": 1 } ) db.trips.createIndex({ "birth year": 1 }) db.trips.createIndex({ "start station id": 1, "birth year": 1 })
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()
Find all documents where the student had an extra credit score:
db.grades.find({ "scores": { "$elemMatch": { "type": "extra credit" } } }).pretty()
Upsert Example
db.iot.updateOne({ "sensor": r.sensor, "date": r.date, "valcount": { "$lt": 48 } }, { "$push": { "readings": { "v": r.value, "t": r.time } }, "$inc": { "valcount": 1, "total": r.value } }, { "upsert": true })
Using the aggregation framework find all documents that have Wifi as one of the amenities``*. Only include* ``price and address in the resulting cursor.
db.listingsAndReviews.aggregate([ { "$match": { "amenities": "Wifi" } }, { "$project": { "price": 1, "address": 1, "_id": 0 }}]).pretty()
Project only the address field value for each document, then group all documents into one document per address.country value.
db.listingsAndReviews.aggregate([ { "$project": { "address": 1, "_id": 0 }}, { "$group": { "_id": "$address.country" }}])
Project only the address field value for each document, then group all documents into one document per address.country value, and count one for each document in each group.
db.listingsAndReviews.aggregate([ { "$project": { "address": 1, "_id": 0 }}, { "$group": { "_id": "$address.country", "count": { "$sum": 1 } } } ])
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()
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()
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()
.pretty()
Usage db.<collection>.find(<query>).pretty() Returns the documents that match the given query formatted for ease of reading.
Find Command
Usage db.<document>.find({"key":"value"})
$elmMatch
Match document that contain an array field with at least one element that matches the specified query criteria. or Projects only the array elements with at least one element that matches the specified criteria. {<field>:{"$elmMatch":{<field>:<value>}}}
_id
"_id" is a unique identifier for a document in a collection. "_id" is a mandatory field in every MonngoDB document. Identical documents can exist in the same collection as long as their _id values are different.
$group Stage
$group Stage is one of the many stages that differentiates the aggregation framework from MQL. With MQL we can MQL we can filter and update data. With the aggregation framework we can compute and reshape data.
Array Operator
$push Allows us to add an element to an array Turns a field into an array field if it was previously a different type. {<array field>:{"$size":<number>}} Return a cursor with all documents where the specified array field is exactly the given length. {<array field>:{"$all":<array>}} Return a cursor with all documents in which the specified array field contains all the given elements regardless of their order in the array.
What is Data Modeling?
A way to organize fields in a document to support your application performance and querying capabilities.
JSON vs BSON Write BSON or JSON in the numbered blanks in the following sentences to make them true: MongoDB stores data in (A)__________________, and you can then view it in (B)___________________ . (C)_______________________ is faster to parse and lighter to store than(D)____________________ . (E)____________________ supports fewer data types than(F)_____________________ .
A)BSON B)JSON C)BSON D)JSON E)JSON F)BSON
Quiz 1: $expr Problem: What are some of the uses for the $ sign in MQL? Check all the answers that apply: A) $ denotes an operator B) $ changes the data type of the given value to a monetary denomination. C) $ makes the world go round. D) $ signifies that you are looking at the value of that field rather than the field name.
Ans) A, D
$push operator
Adds an element to an array field. Usage: {$push:{<field1>:<value1>,...}}
Expressive $expr Query Operator
Allows for more complex queries and for comparing fields within a document. $expr allows the use of aggregation expression within the query language {$expr:{<expression>}} $expr allows us to use variables and conditional statement.
Which is the most succinct query to return all documents from the sample_training.inspections collection where the inspection date is either "Feb 20 2015", or "Feb 21 2015" and the company is not part of the "Cigarette Retail Dealer - 127" sector? A) db.inspections.find( { "$or": [ { "date": "Feb 20 2015" }, { "date": "Feb 21 2015" } ], "sector": { "$ne": "Cigarette Retail Dealer - 127" }}).pretty() B) db.inspections.find( { "$or": [ { "date": "Feb 20 2015" }, { "date": "Feb 21 2015" }], "$not": { "sector": "Cigarette Retail Dealer - 127" }}).pretty() C) db.inspections.find( { "$and": [ { "$or": [ { "date": "Feb 20 2015" }, { "date": "Feb 21 2015" } ] }, {"sector": { "$ne":"Cigarette Retail Dealer - 127" }}] }).pretty()
Ans) A
Quiz 1: Deleting Documents Problem: The sample dataset contains a few databases that we will not use in this course. Clean up your Atlas cluster and get rid of all the collections in these databases: sample_analytics sample_geospatial sample_weatherdata Does removing all collections in a database also remove the database? A) Yes B) No
Ans) A When all collections are dropped from a database, the database no longer appears in the list of databases when you run show dbs.
Querying an array field using
An array returns only exact array matches. A single element will return all documents where the specified array field contains this given element.
What is a Index?
An index in a database is, by its function, similar to an index in a book, where you have an alphabetical list of names, subjects, etc., with references to the places where they occur, typically found at the end of a book. In a database - special data structure that stores a small portion of the collection's data set in an easy to traverse form.
$group
An operator that takes the incoming stream of data, and siphons it into multiple distinct reservoirs. Example: Which countries are listed in the sample_airbnb.listingsAndReviews collection? {$group:{_id:<expression>,// Group By expression <field1>:{<accumulator1>:<expression1>}, ...}}
Problem: What is data modeling? Choose the best answer: A) a way to organize fields in a document to support your application performance and querying capabilities B) a way to build your application based on how your data is stored C) a way to show off your amazing data to everyone using graphs and illustrations D) a way to decide whether to store your data in the cloud or locally
Ans) A
Quiz: Array Operators and Projection Problem: Which of the following queries will return only the names of companies from the sample_training.companies collection that had exactly 8 funding rounds? Choose the best answer: A) db.companies.find({ "funding_rounds": { "$size": 8 } }, { "name": 1, "_id": 0 }) B) db.companies.find({ "funding_rounds": { "$size": 8 } }, { "name": 1 }) C) db.companies.find({ "funding_rounds": { "$size": 8 } }, { "name": 0, "_id": 1 })
Ans) A db.companies.find({ "funding_rounds": { "$size": 8 } }, { "name": 1, "_id": 0 }) This is correct. Since we are looking to only view the names of companies then we have to explicitly exclude the _id in the projection.
Quiz: Aggregation Framework Problem: What are the differences between using aggregate() and find()? Check all answers that apply: A) aggregate() allows us to compute and reshape data in the cursor. B) find() can do what the aggregate() can do and more C) find() allows us to compute and reshape data in the cursor. D) aggregate() can do what find() can and more.
Ans) A, D aggregate() can do what find() can and more. This is correct. Any find() query can be translated into an aggregation pipeline equivalent, but not every aggregation pipeline can be translated into a find() query. find() can do what the aggregate() can do and more. This is incorrect. Any find() query can be translated into an aggregation pipeline equivalent, but not every aggregation pipeline can be translated into a find() query. find() allows us to compute and reshape data in the cursor. This is incorrect. This is incorrect, the only way for us to compute and reshape data using find(), is by using the aggregation framework stages within the find() method. aggregate() allows us to compute and reshape data in the cursor. This is correct. The aggregation framework allows us to compute and reshape data via using stages like $group, $sum, and others.
Atlas Features Problem: What actions are available to you via the Aggregation Builder in the Atlas Data Explorer? Check all answers that apply: A) Syntax for each selected aggregation stage. B) Export pipeline to a programming language. C) An indication of which indexes are used by the pipeline. D) A preview of the data in the pipeline at each selected stage.
Ans) A,B, and D
Quiz 1: sort() and limit() Problem: Which of the following commands will return the name and founding year for the 5 oldest companies in the sample_training.companies collection? Check all answers that apply: A) db.companies.find({ "founded_year": { "$ne": null }}, { "name": 1, "founded_year": 1 } ).sort({ "founded_year": 1 }).limit(5) B) db.companies.find({ },{ "name": 1, "founded_year": 1 } ).sort({ "founded_year": 1 }).limit(5) C) db.companies.find({ "founded_year": { "$ne": null }}, { "name": 1, "founded_year": 1 } ).limit(5).sort({ "founded_year": 1 }) D) db.companies.find({ "name": 1, "founded_year": 1 } ).sort({ "founded_year": 1 }).limit(5)
Ans) A,C db.companies.find({ "founded_year": { "$ne": null }}, { "name": 1, "founded_year": 1 } ).sort({ "founded_year": 1 }).limit(5) This is correct. We first must filter out the documents where the founded year is not null, then project the fields that we are looking for, which is name, and founded_year in this case. Then we sort the cursor in increasing order, so the first results will have the smallest value for the founded_year field. Finally, we limit the results to our top 5 documents in the cursor, thus getting the 5 oldest companies in this collection. db.companies.find({ },{ "name": 1, "founded_year": 1 } ).sort({ "founded_year": 1 }).limit(5) This is incorrect. This command is missing the search criteria, which means that null values will be included. Excluding the null values isn't always necessary, because it isn't best practice to store these types of values in MongoDB anyway. So there are lots of collections out there without null values. However, this collection contains nulls, and we need to exclude them from our cursor, or else they'll be our smallest value when we sort. db.companies.find({ "founded_year": { "$ne": null }}, { "name": 1, "founded_year": 1 } ).limit(5).sort({ "founded_year": 1 }) This is correct. While the limit() and sort() methods are not listed in the correct order, MongoDB flips their order when executing the query, delivering the results that the question prompt is looking for. db.companies.find({ "name": 1, "founded_year": 1 } ).sort({ "founded_year": 1 }).limit(5) This is incorrect. It looks like the query filter has the values that the projection part of the find() command should have, and there is no projection used in this command. So this command will return all companies where the name is equal to 1 and the founded_year is also equal to 1, which unfortunately misses the requirements of the question prompt.
Quiz: Insert Order Problem: Which of the following commands will successfully insert 3 new documents into an empty pets collection? A) db.pets.insert([{ "_id": 1, "pet": "cat" }, { "_id": 2, "pet": "dog" }, { "_id": 3, "pet": "fish" }, { "_id": 3, "pet": "snake" }]) B) db.pets.insert([{ "_id": 1, "pet": "cat" }, { "_id": 1, "pet": "dog" }, { "_id": 3, "pet": "fish" }, { "_id": 4, "pet": "snake" }], { "ordered": true }) C) db.pets.insert([{ "_id": 1, "pet": "cat" }, { "_id": 1, "pet": "dog" }, { "_id": 3, "pet": "fish" }, { "_id": 4, "pet": "snake" }], { "ordered": false }) D) db.pets.insert([{ "pet": "cat" }, { "pet": "dog" }, { "pet": "fish" }])
Ans) A,C, and D db.pets.insert([{ "pet": "cat" }, { "pet": "dog" }, { "pet": "fish" }]) This is correct. The _id field is not specified in any of these documents, which means that it will be created for each automatically, and it will be unique. db.pets.insert([{ "_id": 1, "pet": "cat" }, { "_id": 1, "pet": "dog" }, { "_id": 3, "pet": "fish" }, { "_id": 4, "pet": "snake" }], { "ordered": true }) This is incorrect. There is a duplicate id issue between the "cat" and "dog" documents. While there are still 3 documents with unique _id values, the duplicate key error will appear on the second document in the array, and will prevent the operation from reaching the other documents. This will happen due to the ordered nature of this insert operation. db.pets.insert([{ "_id": 1, "pet": "cat" }, { "_id": 1, "pet": "dog" }, { "_id": 3, "pet": "fish" }, { "_id": 4, "pet": "snake" }], { "ordered": false }) This is correct. This insert is unordered, which means that each document with a unique _id value will get inserted into the collection, which would make a total of 3 inserted documents. db.pets.insert([{ "_id": 1, "pet": "cat" }, { "_id": 2, "pet": "dog" }, { "_id": 3, "pet": "fish" }, { "_id": 3, "pet": "snake" }]) This is correct. While there is a duplicate key error between the "fish" and "snake" documents, it occurs at the very end of the insert operation, because this insert is ordered by default. As a result the first 3 documents will get inserted and the last one will create a duplicate key error.
Lab: Aggregation Framework Problem: To complete this exercise connect to your Atlas cluster using the in-browser IDE space at the end of this chapter. What room types are present in the sample_airbnb.listingsAndReviews collection? Check all answers that apply: A) Private room B) Apartment C) Kitchen D) Living Room E) Entire home/apt F) House G) Shared Room
Ans) A,E, and G db.listingsAndReviews.aggregate([ { "$group": { "_id": "$room_type" } }])
Quiz 2: Deleting Documents Problem: Which of the following commands will delete a collection named villains? A) db.villains.dropAll() B) db.villains.drop() C) db.villains.delete()
Ans) B db.villains.drop() This is correct. Instead of deleting all documents, you can drop the collection, and this is the right syntax for it. db.villains.delete() This is incorrect. This syntax is incorrect and will produce an error. db.villains.dropAll() This is incorrect. This syntax is incorrect and will produce an error.
Which of the following are true about the mongo shell? A) mongo shell automatically returns an ordered list of results B) It is a fully functioning JavaScript interpreter C) It allows you to interact with your MongoDB instance without using a Graphical User Interface
Ans) B, C
Quiz 2: $expr Problem: Which of the following statements will find all the companies that have more employees than the year in which they were founded? Check all answers that apply: A) db.companies.find({ "number_of_employees": { "$gt": "$founded_year" } }).count() B) db.companies.find( { "$expr": { "$lt": [ "$founded_year", "$number_of_employees" ] } } ).count() C) db.companies.find( { "$expr": { "$gt": [ "$founded_year", "$number_of_employees" ] } } ).count() D) db.companies.find( { "$expr": { "$gt": [ "$number_of_employees", "$founded_year" ]} } ).count()
Ans) B, D db.companies.find( { "$expr": { "$gt": [ "$number_of_employees", "$founded_year" ] }}).count() This is correct. We have to use the $expr operator to compare the two field values within each document. Using the $expr operator is the reason why we have to use the aggregation syntax for the comparison operator. db.companies.find( { "$expr": { "$lt": [ "$founded_year", "$number_of_employees" ]}}).count() This is correct. When we swap the compared fields and use the $lt less than operator instead of the $gt greater than operator, the result should still be correct.
Quiz: Updating Documents in the shell Problem: Given a pets collection where each document has the following structure and fields: { "_id": ObjectId("5ec414e5e722bb1f65a25451"), "pet": "wolf", "domestic?": false, "diet": "carnivorous", "climate": ["polar", "equatorial", "continental", "mountain"] } Which of the following commands will add new fields to the updated documents? A) db.pets.updateMany({ "pet": "cat" }, { "$set": { "climate": "continental" }}) B) db.pets.updateMany({ "pet": "cat" }, { "$set": { "type": "dangerous", "look": "adorable" }}) C) db.pets.updateMany({ "pet": "cat" }, { "$push": { "climate": "continental", "look": "adorable" } }) D) db.pets.updateMany({ "pet": "cat" }, {"$set": { "domestic?": true, "diet": "mice" }})
Ans) B,C db.pets.updateMany({ "pet": "cat" }, { "$set": { "type": "dangerous", "look": "adorable" }}) This is correct. Fields "type" and "look" do not exist in the existing documents in the collection, so this command will create new fields with the given values for all documents that have the "pet" field equal to "cat". db.pets.updateMany({ "pet": "cat" }, { "$push": { "climate": "continental", "look": "adorable" } }) This is correct. While the "climate" field is already present in the documents in this collection, the field "look" is new, and this command will create a new array field called "look", with one element "adorable" in it. db.pets.updateMany({ "pet": "cat" }, {"$set": { "domestic?": true, "diet": "mice" }}) This is incorrect. Both "domestic?" and "diet" are fields that exist in all documents in this collection. This update command will update the values of existing fields, rather than create new ones. db.pets.updateMany({ "pet": "cat" }, { "$set": { "climate": "continental" }}) This is incorrect. The field "climate" already exists in all documents in this collection, however, it is an array field. This doesn't mean that the $set operator cannot be used here. The set operator will simply change the array value of the "climate" field to a single string element "continental".
Quiz: Upsert Problem: How does the upsert option work? Check all answers that apply: A) It is used with the update operator, and needs to have its value specified every time that the update operator is called. B) By default upsert is set to false. C) When upsert is set to false and the query predicate returns an empty cursor then there will be no updated documents as a result of this operation. D) When upsert is set to true and the query predicate returns an empty cursor, the update operation creates a new document using the directive from the query predicate and the update predicate.
Ans) B,C, and D It is used with the update operator, and needs to have its value specified every time that the update operator is called. This is incorrect. The upsert option only needs its value specified if you want to change the default false setting to true. By default upsert is set to false. This is correct. If the upsert option is not specified, then it will have the value of false by default. When upsert is set to true and the query predicate returns an empty cursor, the update operation creates a new document using the directive from the query predicate and the update predicate. This is correct. When upsert is set to true it can perform an insert if the query predicate doesn't return a matching document. When upsert is set to false and the query predicate returns an empty cursor then there will be no updated documents as a result of this operation. This is correct. When upsert is set to false an update will happen only when the query predicate is matched with a document from the collection.
Find Command What does it do in the mongo shell? A) Warns about imminent termination B) Interferes with the task C) Iterates through the cursor results D) It is not a mongo shell command
Ans) C
Quiz: Atlas Products Problem: What is MongoDB Charts? Choose the best answer: A) A feature that displays performance data about your Atlas Cluster. B) An app that allows you to embed visualizations that are created in other applications into your website. C) A product that helps you build visualizations of the data stored in your Atlas Cluster.
Ans) C
Quiz 2: sort() and limit() Problem: To complete this exercise connect to your Atlas cluster using the in-browser IDE space at the end of this chapter. In what year was the youngest bike rider from the sample_training.trips collection born? Choose the best answer: A) 1885 B) 1998 C) 1999 D) 2000
Ans) C 1999 This is correct. To get this result you need to run the following query. The most important part is to first filter out all empty string values, in order to get actual year values. You don't have to use projection here, but we added it so that it is easier to see the answer. The sort order should be in decreasing order because we're looking for the youngest person, therefore the highest year value. You don't have to add the limit() method to get this result, but we also added it to make it easier to immediately see the result. db.trips.find({ "birth year": { "$ne":"" } }, { "birth year": 1 }).sort({ "birth year": -1 }).limit(1) 1885 This is incorrect. You may have gotten this result if you sorted in increasing order instead of decreasing order. 2000 1998 These options are incorrect,
Quiz: Querying Arrays and Sub-documents Problem: Which of the following queries will return the names and addresses of all listings from the sample_airbnb.listingsAndReviews collection where the first amenity in the list is "Internet"? Choose the best answer A) db.listingsAndReviews.find({ "amenities.$0": "Internet" }, { "name": 1, "address": 1 }).pretty() B) db.listingsAndReviews.find({ "amenities.[0]": "Internet" }, { "name": 1, "address": 1 }).pretty() C) db.listingsAndReviews.find({ "amenities.0": "Internet" }, { "name": 1, "address": 1 }).pretty()
Ans) C db.listingsAndReviews.find({ "amenities.0": "Internet" }, { "name": 1, "address": 1 }).pretty() This is correct. To reach the first element in an array field we need to use dot notation and the element's position which is zero for the first element in most arrays in most languages. For the projection part, we don't need to explicitly exclude the _id field because the question prompt isn't terribly strict about that.
Quiz: Introduction to Indexes Problem: Jameela often queries the sample_training.routes collection by the src_airport field like this: db.routes.find({ "src_airport": "MUC" }).pretty() Which command will create an index that will support this query? Choose the best answer: A) db.routes.makeIndex({ "src_airport": 1 }) B) db.routes.generateIndex({ "src_airport": -1 }) C) db.routes.createIndex({ "src_airport": -1 }) D) db.routes.addIndex({ "src_airport": 1 })
Ans) C db.routes.createIndex({ "src_airport": -1 }) This is correct. It doesn't really matter whether the index was created in increasing or decreasing order when it is a simple single-field index.
How does the value of _id get assigned to a document? Check all answers that apply: A) When a document is inserted a random field is picked to serve as the _id field. B) _id field values are sequential integer values. C) You can select a non ObjectId type value when inserting a new document, as long as that value is unique to this collection. D) It is automatically generated as an ObjectId type value.
Ans) C,D It is automatically generated as an ObjectId type value. This is correct. When inserting a document via the Data Explorer in Atlas, the _id value is already generated as an ObjectID type, while the rest of the fields are not. You can select a non ObjectId type value when inserting a new document, as long as that value is unique to this collection. This is correct. MongoDB generates a value, so that there is one just in case. You can definitely change the default value to a different value or data type, as long as they are unique to this collection and not an array data type. When a document is inserted a random field is picked to serve as the _id field. This is incorrect. MongoDB adds an _id field to any inserted document if it doesn't have one, and it does not utilize other fields for this purpose. _id field values are sequential integer values. This is incorrect. You can assign the _id field values to be sequential integer values, but it is not the default behavior, nor is it best practice.
Quiz: Updating Documents Problem: MongoDB has a flexible data model, which means that you can have fields that contain documents, or arrays as their values. Select any invalid MongoDB documents from the given choices: A) { "_id": 1, "pet": "cat", "attributes": [ { "coat": "fur", "type": "soft" }, { "defense": "claws", "location": "paws", "nickname": "murder mittens" } ], "name": "Furball" } B) { "_id": 1, "pet": "cat", "fur": "soft", "claws": "sharp", "name": "Furball" } C) { "_id": 1, "pet": "cat", "attributes": { "coat": "soft fur", "paws": "cute but deadly" }, "name": "Furball" } D) None of the above
Ans) D { "_id": 1, "pet": "cat", "attributes": [ { "coat": "fur", "type": "soft" }, { "defense": "claws", "location": "paws", "nickname": "murder mittens" } ], "name": "Furball" } This is incorrect. This is a valid MongoDB document, which uses correct JSON. One of the elements is an array of documents just like in the example that we saw in the previous lesson. { "_id": 1, "pet": "cat", "fur": "soft", "claws": "sharp", "name": "Furball" } This is incorrect. This is a valid MongoDB document which uses correct JSON notation. { "_id": 1, "pet": "cat", "attributes": { "coat": "soft fur", "paws": "cute but deadly" }, "name": "Furball" } This is incorrect. This is a valid MongoDB document, which uses correct JSON. One of the elements is a document object just like the documents in the sample_training.inspections collection. None of the above. This is correct. All the documents presented in the choices are valid MongoDB documents.
Quiz: Compass Problem: What is MongoDB Compass? Choose the best answer: A) MongoDB's Graphical User Interface Product B) A term used to denote an object that stores geospatial coordinates in the collection. C) A type of map-based chart that is available with MongoDB Charts
Ans)A
Which of the following queries will return all listings that have "Free parking on premises", "Air conditioning", and "Wifi" as part of their amenities, and have at least 2 bedrooms in the sample_airbnb.listingsAndReviews collection? Choose the best answer: A) db.listingsAndReviews.find( { "amenities": [ "Free parking on premises", "Wifi", "Air conditioning" ]}, "bedrooms": { "$gte": 2 } } ).pretty() B) db.listingsAndReviews.find( { "amenities": { "$all": [ "Free parking on premises", "Wifi", "Air conditioning" ] }, "bedrooms": { "$lte": 2 } }).pretty() C) db.listingsAndReviews.find( { "amenities": "Free parking on premises", "amenities": "Wifi", "amenities": "Air conditioning", "bedrooms": { "$gte": 2 } }).pretty() D) db.listingsAndReviews.find( { "amenities": { "$all": [ "Free parking on premises", "Wifi", "Air conditioning" ] }, "bedrooms": { "$gte": 2 } } ).pretty()
Ans)D db.listingsAndReviews.find( { "amenities": { "$all": [ "Free parking on premises", "Wifi", "Air conditioning" ] }, "bedrooms": { "$gte": 2 } }).pretty() This is correct. We have to use the $all array operator In order to get all documents where the amenities array contains these three elements regardless of their order. To get the desired number of bedrooms the best operator to use is $gte, since it includes 2, but also shows other listings with more bedrooms.
What is Atlas? How is MongoDB Atlas related to MongoDB the Database?
Atlas has many tools and services within it that are built specifically for the MongoDB database. They both are MongoDB products.
Comparison Operator
Comparison operators specifically allow us to find data within a certain range. { <field>:{<operator>:<value>}} When comparision operator is not specified $eq is used as the default operator. Example: $eq = Equal to $ne = Not Equal to $gt > Greater Than $lt < Less Than $gte >= Greater Than or Equal To $lte <= Less Than or Equal To
Updating Documents- mongo shell
Connect to your Atlas Cluster >>mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin" Use the sample_training database as your database in the following commands. >>use sample_training Find all documents in the zips collection where the zip field is equal to "12434". >>db.zips.find({ "zip": "12534" }).pretty() Find all documents in the zips collection where the city field is equal to "HUDSON". >>db.zips.find({ "city": "HUDSON" }).pretty() Find how many documents in the zips collection have the city field equal to "HUDSON". >>db.zips.find({ "city": "HUDSON" }).count() Update all documents in the zips collection where the city field is equal to "HUDSON" by adding 10 to the current value of the "pop" field. >>db.zips.updateMany({ "city": "HUDSON" }, { "$inc": { "pop": 10 } }) Update a single document in the zips collection where the zip field is equal to "12534" by setting the value of the "pop" field to 17630. >>db.zips.updateOne({ "zip": "12534" }, { "$set": { "pop": 17630 } }) Update a single document in the zips collection where the zip field is equal to "12534" by setting the value of the "population" field to 17630. >>db.zips.updateOne({ "zip": "12534" }, { "$set": { "population": 17630 } }) Find all documents in the grades collection where the student_id field is 151 , and the class_id field is 339. >>db.grades.find({ "student_id": 151, "class_id": 339 }).pretty() Find all documents in the grades collection where the student_id field is 250 , and the class_id field is 339. >>db.grades.find({ "student_id": 250, "class_id": 339 }).pretty() Update one document in the grades collection where the student_id is ``250`` *, and the class_id field is 339 , by adding a document element to the "scores" array. >>db.grades.updateOne({ "student_id": 250, "class_id": 339 }, { "$push": { "scores": { "type": "extra credit", "score": 100 } } })
Update Operator
Enable us to modify data in the database. Example: $inc, $set, $unset
Upsert
Everything in MQL that is used to locate a document in a collection can also be used to modify this document. db.collection.updateOne({<query to locate>},{<update>}) Upsert is a hybrid of update and insert, it should only be used when it is needed. db.collection.updateOne({<query>},{<update>},{"upsert":true}) Upsert is a good option for conditional updates when you want a new document instead of an updated one. In all other cases, where you are just looking to make an update to an existing document or are looking to insert a brand new document with default upsert:false and insert command.
updateMany()
If there are multiple documents that match a given criteria, it will update all the documents that match a given query.
limit() without sort()
If you're looking for some specific number of results that best match your query you can use limit(). The caveat with limit() is that if you use limit() without sort(), you will most likely get some results without any guarantee of its order. Similarly, if you use limit() before you sort(), you might miss some of the data that you meant to sort and include in the result set. cursor.limit().sort() means cursor.sort().limit() regardless of the order in which you type these. Example: use sample_training db.zips.find().sort({ "pop": 1 }).limit(1) db.zips.find({ "pop": 0 }).count() db.zips.find().sort({ "pop": -1 }).limit(1) db.zips.find().sort({ "pop": -1 }).limit(10) db.zips.find().sort({ "pop": 1, "city": -1 })
Cursor Method
Sort() and limit() are cursor methods. Some other cursor methods are sort(), limit(),pretty(), count(). A cursor method is not applied to the data, that is stored in the database. It is instead applied to the results set that lives in the cursor. After the cursor is populated with the filter data that's the result of the find command, we can then apply the sort(0 method, which will sort the data based on the criteria that we provided.
Inserting New Documents - insert() order
Insert three test documents: >>db.inspections.insert([ { "test": 1 }, { "test": 2 }, { "test": 3 } ]) Insert three test documents but specify the _id values: >>db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 }, { "_id": 3, "test": 3 }]) Find the documents with _id: 1 >>db.inspections.find({ "_id": 1 }) Insert multiple documents specifying the _id values, and using the "ordered": false option. >>db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 }, { "_id": 3, "test": 3 }],{ "ordered": false }) Insert multiple documents with _id: 1 with the default "ordered": true setting >>db.inspection.insert([{ "_id": 1, "test": 1 },{ "_id": 3, "test": 3 }]) View collections in the active db >>show collections Switch the active db to training >>use training View all available databases >>show dbs
findOne()
It returns the first document that happens to match the given query.
Logical Operator
Logic operator allow us to be more granular in our search for data. $and - Match all of the specified query clauses. $or - At least on of the query clauses is matched $nor- Fail to match both given clauses. $not- Negates the query requirement. General syntax: {<operator>:[{statement1},{statement2},....]} Syantx for Not {$not: {statement}} $and is used as the default operator when an operator is not specified. Explicitly use $and when you need to include the same operator more than once in a query.
Deleting Documents and Collections
Look at all the docs that have test field equal to 1. >>db.inspections.find({ "test": 1 }).pretty() Look at all the docs that have test field equal to 3. >>db.inspections.find({ "test": 3 }).pretty() Delete all the documents that have test field equal to 1. >>db.inspections.deleteMany({ "test": 1 }) Delete one document that has test field equal to 3. >>db.inspections.deleteOne({ "test": 3 }) Inspect what is left of the inspection collection. >>db.inspection.find().pretty() View what collections are present in the sample_training collection. >>show collections Drop the inspection collection. >>db.inspection.drop()
MQL Syntax Vs Aggregation Syntax
MQL Syantax { <field>: {<operator>:<value>}} Aggregation Syntax: {<operator>:{<field>,<value>}}
Querying arrays and sub-documents
MQL uses dot- notation to specify the address of nested elements in a document. To use dot-notation in array specify the position of the element of the array. db.collection.find({"field 1.other field.also a field":"value"})
Indexes
Make queries even more efficient Are one of the most impactful ways to improve query performance.
Projection Syntax
Specifies which fields should or should not be included in the result cursor. Do not combine 1s and 0s in a projection. db.<collection>.find({<query>},{<projection>}) 1-include the field 0-exclude the field Use only 1s or only 0s db.<collection>.find({<query>},{<field1>:1,<field2>:1}) or db.<collection>.find({<query>},{<field1>:0,<field2>:0}) Exception: db.<collection>.find({<query>},{<field1>:1,"_id":0})
Inserting New Documents
Step one: Connect to the Atlas cluster >>mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin" Step two: navigate to the database that we need: >>use sample_training Step three, get a random document from the collection: >>db.inspections.findOne(); Step four, copy this random document, and try to insert in into the collection: >>db.inspections.insert({ "_id" : ObjectId("56d61033a378eccde8a8354f"), "id" : "10021-2015-ENFO", "certificate_number" : 9278806, "business_name" : "ATLIXCO DELI GROCERY INC.", "date" : "Feb 20 2015", "result" : "No Violation Issued", "sector" : "Cigarette Retail Dealer - 127", "address" : { "city" : "RIDGEWOOD", "zip" : 11385, "street" : "MENAHAN ST", "number" : 1712 } }) db.inspections.insert({ "id" : "10021-2015-ENFO", "certificate_number" : 9278806, "business_name" : "ATLIXCO DELI GROCERY INC.", "date" : "Feb 20 2015", "result" : "No Violation Issued", "sector" : "Cigarette Retail Dealer - 127", "address" : { "city" : "RIDGEWOOD", "zip" : 11385, "street" : "MENAHAN ST", "number" : 1712 } }) db.inspections.find({"id" : "10021-2015-ENFO", "certificate_number" : 9278806}).pretty()
What is MongoDB aggregation framework?
The MongoDB aggregation framework is a way to query the data from MongoDB. It helps us to break complex logic into a simple set of sequential operations. Just like a waterfall model, Output from one stage is fed as an input for the next stage until the desired result is achieved. Aggregation framework allows us to do incredible things with data. collection =>$project=>$match=>$group=>$sort=>Result ==================================> Pipeline----Series of stages Sample workflow on the aggregation framework
What is the MongoDB Database? Select the statements that together help build the most complete definition of the MongoDB database:
The MongoDB database is an organized way to store and access data. MongoDB is a NoSQL database that uses documents to store data in an organized way.
What is a URI string?
Uniform Resource Identifier mongodb+srv://user:[email protected]/database
.count()
Usage db.<collection>.find(<query>).count() Returns the number of documents that match the given query.
How to insert multiple documents by using an array:
db.collection.insert([{<doc1>},{<doc2>}]) Use {"ordered": false} to disable the default ordered insert.
When to Index?
You should build an index to support your queries. For example: Given If I'm running queries on these Trips collection and find that often by the birth year, then I should have an index that supports these queries. db.trips.find({"birth year":1989}) This query filters data by the value of the birth year fields. db.trips.find({"start station id":476}).sort("birth year":1) This query sorts by the value of that field. Both query could benefit from Index: Index Query db.trips.createIndex({"birth year":1}) This query create index based on the birth year field in increasing order. Now that we have this index where we issue this query, MongoDB doesn't have to look at every document to get the needed results. db.trips.find({"birth year":1989})
Find all documents with exactly 20 amenities which include all the amenities listed in the query array:
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()
Find all documents with exactly 20 amenities which include all the amenities listed in the query array, and display their price and address: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()
Find all documents that have Wifi as one of the amenities only includes price and address in the resulting cursor.
db.listingsAndReviews.find({"amenities":"Wifi"},{"price":1,"address":1,"_id":0}).pretty() db.listingsAndReviews.aggregate([{$match:{"amenities":"Wifi"}},{$project:{"price":1,"address":1,"_id":0}}])
Find one document in the collection and only include the address field in the resulting cursor.
db.listingsAndReviews.findOne({ },{ "address": 1, "_id": 0 })
Find all documents where the trip started and ended at the same station:
db.trips.find({ "$expr": { "$eq": [ "$end station id", "$start station id"] } }).count()
deleteMany()
deleteMany() works as a updateMany() but only update happens is the documents gets removed from the database and it only deletes documents that match a given query.
deleteOne()
deleteOne() works as a updateOne() but only update happens is that the document gets removed from the database, and it only deletes documents that match a given query. The only time when deleteOne is a good approach to deleting documents is when we are querying by the underscore id value, thus guaranteeing that this is the only document matching this query. Otherwise we are running the risk of deleting one of a few or one of many documents that we needed to delete.
Export data in JSON
mongoexport --uri "<Atlas Cluster URI>" mongoexport --collection=<collection name> mongoexport --out=<filename>.json
Which of the following commands will add a collection that is stored in animals.json to an Atlas cluster? A) mongoimport B) mongodump C) mongoexport D) mongorestore
mongoimport
How to import and export data in JSON?
mongoimport (mongoimport can import data from JSON, and other supported non BSON formats.) mongoexport (mongoexport can export data from JSON by making a copy of the data outside the atlas cluster, rather than adding a collection to the atlas cluster.)
Import data in JSON
mongoimport --uri "<Atlas Cluster URI>" mongoimport --drop=<filename>.json
How to import and export data in BSON?
mongorestore (mongorestore imports data from a mongodump created BSON format.) mongodump (mongodump exports data in its raw BSON form.)
Import data in BSON
mongorestore --uri "<Atlas Cluster URI>" mongorestore --drop dump
How to see collections in database?
show collections
Upsert: true or Upsert:False?
upsert:true Conditional updates Upsert:false Update an existing document. Insert a brand new document
How to select the database in mongoDB?
use database
Array Operators and Sub-Documents
use sample_training db.trips.findOne({ "start station location.type": "Point" }) db.companies.find({ "relationships.0.person.last_name": "Zuckerberg" }, { "name": 1 }).pretty() db.companies.find({ "relationships.0.person.first_name": "Mark", "relationships.0.title": { "$regex": "CEO" } }, { "name": 1 }).count() db.companies.find({ "relationships.0.person.first_name": "Mark", "relationships.0.title": {"$regex": "CEO" } }, { "name": 1 }).pretty() db.companies.find({ "relationships": { "$elemMatch": { "is_past": true, "person.first_name": "Mark" } } }, { "name": 1 }).pretty() db.companies.find({ "relationships": { "$elemMatch": { "is_past": true, "person.first_name": "Mark" } } }, { "name": 1 }).count()
Collections and databases are created when they are being used:
use tools followed by db.tractors.insert({<tractor doc>}) creates the tools.tractors namespace.
Find which student ids are > 25 and < 100 in the sample_training.grades collection.
{"$and":{"student_id":{"$gt":25}}, {"student_id":{"$lt":100}}]}
What is JSON? Which of the following document is correct JSON? A) ["name" : "Devi", "age": 19, "major": "Computer Science"] B){"name" : "Devi", "age": 19, "major": "Computer Science"} C){name : "Devi", age: 19, major: "Computer Science"}
{"name" : "Devi", "age": 19, "major": "Computer Science"}
BSON (Binary JSON)
§ Bridges the gap between binary representation and JSON format optimized for: o Speed o Space o Flexibility § MongoDB stores data in BSON internally and over the network. § BSON provides additional features like speed and flexibility.