MongoDB is a document database that stores data in BSON format, allowing for flexible document structures similar to JSON. It can be installed locally or hosted in the cloud using MongoDB Atlas, and offers various query and update methods, including CRUD operations and aggregation pipelines. Key features include collections instead of tables, support for various data types, and capabilities for data transformation and querying.
MongoDB is a document database that stores data in BSON format, allowing for flexible document structures similar to JSON. It can be installed locally or hosted in the cloud using MongoDB Atlas, and offers various query and update methods, including CRUD operations and aggregation pipelines. Key features include collections instead of tables, support for various data types, and capabilities for data transformation and querying.
MongoDB is a document database. It MongoDB stores data in flexible
stores data in a type of JSON format called documents. Instead of having multiple BSON. tables you can simply keep all of your A record in MongoDB is a document, related data together. This makes reading which is a data structure composed of key your data very fast. value pairs similar to the structure of JSON You can still have multiple groups of data objects. too. In MongoDB, instead of tables these A MongoDB Document are called collections. Records in a MongoDB database are called documents, and the field values may Local vs Cloud Database include numbers, strings, booleans, arrays, MongoDB can be installed locally, which or even nested documents. will allow you to host your own MongoDB Example Document: server on your hardware. This requires { you to manage your server, upgrades, and title: "Post Title 1", any other maintenance. body: "Body of post.", You can download and use the MongoDB category: "News", open source Community Server on your likes: 1, hardware for free. tags: ["news", "events"], However, for this course we are going to date: Date() use MongoDB Atlas, a cloud database } platform. This is much easier than hosting your own local database. MongoDB MongoDB is a document database and can MongoDB Query API be installed locally or hosted in the cloud. The MongoDB Query API is the way you will interact with your data. SQL vs Document Databases The MongoDB Query API can be used in SQL databases are considered relational two ways: databases. They store related data in • CRUD Operations separate tables. When data is needed, it is • Aggregation Pipelines queried from multiple tables to join the data back together. MongoDB Query API Uses MongoDB is a document database which You can use the MongoDB Query API to is often referred to as a non-relational perform: database. This does not mean that • Adhoc queries with mongosh, relational data cannot be stored in Compass, VS Code, or a MongoDB document databases. It means that driver for the programming relational data is stored differently. A language you use. better way to refer to it is as a non-tabular • Data transformations using database. aggregation pipelines. MONGODB NOTES • Document join support to combine Create Collection using mongosh data from different collections. There are 2 ways to create a collection. • Graph and geospatial queries. Method 1 • Full-text search. You can create a collection using • Indexing to improve MongoDB the createCollection() database method. query performance. Example • Time series analysis. db.createCollection("posts")
MongoDB mongosh Create Method 2
Database You can also create a collection during Create Database using mongosh the insert process. After connecting to your database Example using mongosh, you can see which We are here assuming object is a valid database you are using by typing db in JavaScript object containing post data: your terminal. db.posts.insertOne(object) If you have used the connection string provided from the MongoDB Atlas This will create the "posts" collection if it dashboard, you should be connected to does not already exist. the myFirstDatabase database. Remember: In MongoDB, a collection is not actually created until it gets content! Show all databases To see all available databases, in your MongoDB mongosh Insert terminal type show dbs. Insert Documents Notice that myFirstDatabase is not listed. There are 2 methods to insert documents This is because the database is empty. An into a MongoDB database. empty database is essentially non- insertOne() existant. To insert a single document, use the insertOne() method. Change or Create a Database This method inserts a single object into You can change or create a new database the database. by typing use then the name of the Note: When typing in the shell, after database. opening an object with curly braces "{" Example you can press enter to start a new line in Create a new database called "blog": the editor without executing the use blog command. The command will execute We are now in the blog database. when you press enter after closing the Remember: In MongoDB, a database is braces. not actually created until it gets content! Example: db.posts.insertOne({ title: "Post Title 1", MONGODB NOTES body: "Body of post.", } category: "News", ]) likes: 1, tags: ["news", "events"], MongoDB mongosh Find date: Date() Find Data }) There are 2 methods to find and select data from a MongoDB Note: If you try to insert documents into a collection, find() and findOne(). collection that does not exist, MongoDB will create the collection automatically. find() To select data from a collection in insertMany() MongoDB, we can use the find() method. To insert multiple documents at once, use This method accepts a query object. If left the insertMany() method. empty, all documents will be returned. This method inserts an array of objects Example: into the database. db.posts.find() Example: db.posts.insertMany([ findOne() { To select only one document, we can use title: "Post Title 2", the findOne() method. body: "Body of post.", This method accepts a query object. If left category: "Event", empty, it will return the first document it likes: 2, finds. tags: ["news", "events"], Note: This method only returns the first date: Date() match it finds. }, Example { db.posts.findOne() title: "Post Title 3", body: "Body of post.", Querying Data category: "Technology", To query, or filter, data we can include a likes: 3, query in our find() or findOne() methods. tags: ["news", "events"], Example: date: Date() db.posts.find( {category: "News"} ) }, { Projection title: "Post Title 4", Both find methods accept a second body: "Body of post.", parameter called projection. category: "Event", This parameter is an object that describes likes: 4, which fields to include in the results. tags: ["news", "events"], date: Date() MONGODB NOTES Note: This parameter is optional. If updateOne() omitted, all fields will be included in the The updateOne() method will update the results. first document that is found matching the Example: provided query. This example will only display Let's see what the "like" count for the post the title and date fields in the results. with the title of "Post Title 1": db.posts.find({}, {title: 1, date: 1}) Example: db.posts.find( { title: "Post Title 1" } ) Notice that the _id field is also included. This field is always included unless Now let's update the "likes" on this post to specifically excluded. 2. To do this, we need to use We use a 1 to include a field and 0 to the $set operator. exclude a field. Example: db.posts.updateOne( { title: "Post Title 1" Example: }, { $set: { likes: 2 } } ) This time, let's exclude the _id field. db.posts.find({}, {_id: 0, title: 1, date: 1}) Check the document again and you'll see that the "like" have been updated. Note: You cannot use both 0 and 1 in the Example: same object. The only exception is db.posts.find( { title: "Post Title 1" } ) the _id field. You should either specify the fields you would like to include or the Insert if not found fields you would like to exclude. If you would like to insert the document if Let's exclude the date category field. All it is not found, you can use other fields will be included in the results. the upsert option. Example: MongoDB mongosh Update Update the document, but if not found Update Document insert it: To update an existing document we can db.posts.updateOne( use { title: "Post Title 5" }, the updateOne() or updateMany() method { s. $set: The first parameter is a query object to { define which document or documents title: "Post Title 5", should be updated. body: "Body of post.", The second parameter is an object category: "Event", defining the updated data. likes: 5, tags: ["news", "events"], date: Date() } }, MONGODB NOTES { upsert: true } MongoDB Query Operators ) There are many query operators that can be used to compare and reference updateMany() document fields. The updateMany() method will update all Comparison documents that match the provided The following operators can be used in query. queries to compare values: Example: • $eq: Values are equal Update likes on all documents by 1. For • $ne: Values are not equal this we will use the $inc (increment) • $gt: Value is greater than another operator: value db.posts.updateMany({}, { $inc: { likes: 1 } • $gte: Value is greater than or equal }) to another value • $lt: Value is less than another Now check the likes in all of the value documents and you will see that they • $lte: Value is less than or equal to have all been incremented by 1. another value • $in: Value is matched within an MongoDB mongosh Delete array Delete Documents Logical We can delete documents by using the The following operators can logically methods deleteOne() or deleteMany(). compare multiple queries. These methods accept a query object. The • $and: Returns documents where matching documents will be deleted. both queries match • $or: Returns documents where
deleteOne() either query matches
• $nor: Returns documents where The deleteOne() method will delete the first document that matches the query both queries fail to match • $not: Returns documents where provided. Example the query does not match db.posts.deleteOne({ title: "Post Title 5" }) Evaluation The following operators assist in deleteMany() evaluating documents. • $regex: Allows the use of regular The deleteMany() method will delete all documents that match the query expressions when evaluating field provided. values • $text: Performs a text search Example: • $where: Uses a JavaScript db.posts.deleteMany({ category: "Technology" }) expression to match documents MONGODB NOTES MongoDB Update Operators }, There are many update operators that can // Stage 2: Group documents by category be used during document updates. and sum each categories likes Fields { The following operators can be used to $group: { _id: "$category", totalLikes: { update fields: $sum: "$likes" } } • $currentDate: Sets the field value } to the current date ]) • $inc: Increments the field value • $rename: Renames the field Sample Data • $set: Sets the value of a field To demonstrate the use of stages in a • $unset: Removes the field from the aggregation pipeline, we will load sample document data into our database. Array The following operators assist with MongoDB Aggregation $group updating arrays. Aggregation $group • $addToSet: Adds distinct elements This aggregation stage groups documents to an array by the unique _id expression provided. • $pop: Removes the first or last Don't confuse this _id expression with element of an array the _id ObjectId provided to each • $pull: Removes all elements from document. an array that match the query Example: • $push: Adds an element to an In this example, we are using the array "sample_airbnb" database loaded from our sample data in the Intro to MongoDB Aggregation Pipelines Aggregations section. Aggregation Pipelines db.listingsAndReviews.aggregate( Aggregation operations allow you to [ { $group : { _id : "$property_type" } } ] group, sort, perform calculations, analyze ) data, and much more. This will return the distinct values from Aggregation pipelines can have one or the property_type field. more "stages". The order of these stages are important. Each stage acts upon the MongoDB Aggregation $limit results of the previous stage. Aggregation $limit Example: This aggregation stage limits the number db.posts.aggregate([ of documents passed to the next stage. // Stage 1: Only find documents that have more than 1 like { $match: { likes: { $gt: 1 } } MONGODB NOTES Example: Note: You cannot use both 0 and 1 in the In this example, we are using the same object. The only exception is "sample_mflix" database loaded from our the _id field. You should either specify the sample data in the Intro to fields you would like to include or the Aggregations section. fields you would like to exclude. db.movies.aggregate([ { $limit: 1 } ]) MongoDB Aggregation $sort This will return the 1 movie from the Aggregation $sort collection. This aggregation stage groups sorts all documents in the specified sort order. MongoDB Aggregation $project Remember that the order of your stages Aggregation $project matters. Each stage only acts upon the This aggregation stage passes only the documents that previous stages provide. specified fields along to the next Example: aggregation stage. In this example, we are using the This is the same projection that is used "sample_airbnb" database loaded from with the find() method. our sample data in the Intro to Example: Aggregations section. In this example, we are using the db.listingsAndReviews.aggregate([ "sample_restaurants" database loaded { from our sample data in the Intro to $sort: { "accommodates": -1 } Aggregations section. }, db.restaurants.aggregate([ { { $project: { $project: { "name": 1, "name": 1, "accommodates": 1 "cuisine": 1, } "address": 1 }, } { }, $limit: 5 { } $limit: 5 ]) } This will return the documents sorted in ]) descending order by This will return the documents but only the accommodates field. include the specified fields. The sort order can be chosen by Notice that the _id field is also included. using 1 or -1. 1 is ascending and -1 is This field is always included unless descending. specifically excluded. We use a 1 to include a field and 0 to exclude a field. MONGODB NOTES MongoDB Aggregation $match }, Aggregation $match { This aggregation stage behaves like a find. $project: { It will filter documents that match the "name": 1, query provided. "avgGrade": 1 Using $match early in the pipeline can } improve performance since it limits the }, number of documents the next stages { must process. $limit: 5 Example: } In this example, we are using the ]) "sample_airbnb" database loaded from This will return the documents along with our sample data in the Intro to a new field, avgGrade, which will contain Aggregations section. the average of each db.listingsAndReviews.aggregate([ restaurants grades.score. { $match : { property_type : "House" } }, { $limit: 2 }, MongoDB Aggregation $count { $project: { Aggregation $count "name": 1, This aggregation stage counts the total "bedrooms": 1, amount of documents passed from the "price": 1 previous stage. }} Example: ]) In this example, we are using the This will only return documents that have "sample_restaurants" database loaded the property_type of "House". from our sample data in the Intro to Aggregations section. MongoDB Aggregation $addFields db.restaurants.aggregate([ Aggregation $addFields { This aggregation stage adds new fields to $match: { "cuisine": "Chinese" } documents. }, Example: { In this example, we are using the $count: "totalChinese" "sample_restaurants" database loaded } from our sample data in the Intro to ]) Aggregations section. This will return the number of documents db.restaurants.aggregate([ at the $count stage as a field called { "totalChinese". $addFields: { avgGrade: { $avg: "$grades.score" } } MONGODB NOTES MongoDB Aggregation $lookup MongoDB Aggregation $out Aggregation $lookup Aggregation $out This aggregation stage performs a left This aggregation stage writes the returned outer join to a collection in the same documents from the aggregation pipeline database. to a collection. There are four required fields: The $out stage must be the last stage of • from: The collection to use for the aggregation pipeline. lookup in the same database Example: • localField: The field in the primary In this example, we are using the collection that can be used as a "sample_airbnb" database loaded from unique identifier in our sample data in the Intro to the from collection. Aggregations section. • foreignField: The field in db.listingsAndReviews.aggregate([ the from collection that can be { used as a unique identifier in the $group: { primary collection. _id: "$property_type", • as: The name of the new field that properties: { will contain the matching $push: { documents from name: "$name", the from collection. accommodates: "$accommodates", Example: price: "$price", In this example, we are using the }, "sample_mflix" database loaded from our }, sample data in the Intro to }, Aggregations section. }, db.comments.aggregate([ { $out: "properties_by_type" }, { ]) $lookup: { The first stage will group properties by from: "movies", the property_type and include localField: "movie_id", the name, accommodates, and price fields foreignField: "_id", for each. The $out stage will create a new as: "movie_details", collection called properties_by_type in the }, current database and write the resulting }, documents into that collection. { $limit: 1 } ]) This will return the movie data along with each comment. MONGODB NOTES Indexing & Search $search: { MongoDB Atlas comes with a full-text index: "default", // optional unless you search engine that can be used to search named your index something other than for documents in a collection. "default" Atlas Search is powered by Apache text: { Lucene. query: "star wars", path: "title" Creating an Index }, We'll use the Atlas dashboard to create an }, index on the "sample_mflix" database }, from the sample data that we loaded in { the Intro to Aggregations section. $project: { 1. From the Atlas dashboard, click on title: 1, your Cluster name then year: 1, the Search tab. } 2. Click on the Create Search } Index button. ]) 3. Use the Visual Editor and click The first stage of this aggregation pipeline Next. will return all documents in 4. Name your index, choose the the movies collection that contain the Database and Collection you want word "star" or "wars" in the title field. to index and click Next. The second stage will project o If you name your index the title and year fields from each "default" you will not have document. to specify the index name in the $search pipeline MongoDB Schema Validation stage. Schema Validation o Choose By default MongoDB has a flexible the sample_mflix database schema. This means that there is no strict and the movies collection. schema validation set up initially. 5. Click Create Search Index and wait Schema validation rules can be created in for the index to complete. order to ensure that all documents in a collection share a similar structure. Running a Query To use our search index, we will use Schema Validation the $search operator in our aggregation MongoDB supports JSON pipeline. Schema validation. Example The $jsonSchema operator allows us to db.movies.aggregate([ define our document structure. { Example: MONGODB NOTES db.createCollection("posts", { Select the data source(s) you would like to validator: { enable the API on and click Enable the $jsonSchema: { Data API. bsonType: "object", required: [ "title", "body" ], Access Level properties: { By default, no access is granted. Select the title: { access level you'd like to grant the Data bsonType: "string", API. The choices are: No Access, Read description: "Title of post - Only, Read and Write, or Custom Access. Required." }, Data API Key In order to authenticate with the Data API, MongoDB Data API you must first create a Data API key. MongoDB Data API Click Create API Key, enter a name for the The MongoDB Data API can be used to key, then click Generate API Key. query and update data in a MongoDB Be sure to copy the API key and save it database without the need for language somewhere safe. You will not get another specific drivers. chance to see this key again. Language drivers should be used when possible, but the MongoDB Data API Sending a Data API Request comes in handy when drivers are not We can now use the Data API to send a available or drivers are overkill for the request to the database. application. In the next example, we'll use curl to find the first document in Read & Write with the MongoDB the movies collection of Data API our sample_mflix database. We loaded The MongoDB Data API is a pre-configured this sample data in the Intro to set of HTTPS endpoints that can be used Aggregations section. to read and write data to a MongoDB Atlas To run this example, you'll need your App database. Id, API Key, and Cluster name. With the MongoDB Data API, you can You can find your App Id in the URL create, read, update, delete, or aggregate Endpoint field of the Data API page in the documents in a MongoDB Atlas database. MongoDB Atlas UI. Example: Cluster Configuration curl --location --request POST In order to use the Data API, you must first 'https://data.mongodb- enable the functionality from the Atlas UI. api.com/app/<DATA API APP From the MongoDB Atlas dashboard, ID>/endpoint/data/v1/action/findOne' \ navigate to Data API in the left menu. --header 'Content-Type: application/json' \ MONGODB NOTES --header 'Access-Control-Request- Request Body Headers: *' \ Example: --header 'api-key: <DATA API KEY>' \ { --data-raw '{ "dataSource": "<data source name>", "dataSource":"<CLUSTER NAME>", "database": "<database name>", "database":"sample_mflix", "collection": "<collection name>", "collection":"movies", "filter": <query filter>, "projection": {"title": 1} "projection": <projection>, }' "sort": <sort expression>, "limit": <number>, Data API Endpoints "skip": <number> In the previous example, we used } the findOne endpoint in our URL. Insert a Single Document There are several endpoints available for Endpoint use with the Data API. POST Base_URL/insertOne All endpoints start with the Base The insertOne endpoint is used to insert a URL: https://data.mongodb- single document into a collection. api.com/app/<Data API App Request Body ID>/endpoint/data/v1/action/ Example: { Find a Single Document "dataSource": "<data source name>", Endpoint "database": "<database name>", POST Base_URL/findOne "collection": "<collection name>", The findOne endpoint is used to find a "document": <document> single document in a collection. } Request Body Example: Insert Multiple Documents { Endpoint "dataSource": "<data source name>", POST Base_URL/insertMany "database": "<database name>", The insertMany endpoint is used to insert "collection": "<collection name>", multiple documents into a collection. "filter": <query filter>, Request Body "projection": <projection> Example: } { "dataSource": "<data source name>", Find Multiple Documents "database": "<database name>", Endpoint "collection": "<collection name>", POST Base_URL/find "documents": [<document>, The find endpoint is used to find multiple <document>, ...] documents in a collection. } MONGODB NOTES
Update a Single Document Delete Multiple Documents
Endpoint Endpoint POST Base_URL/updateOne POST Base_URL/deleteMany Request Body Request Body Example: Example: { { "dataSource": "<data source name>", "dataSource": "<data source name>", "database": "<database name>", "database": "<database name>", "collection": "<collection name>", "collection": "<collection name>", "filter": <query filter>, "filter": <query filter> "update": <update expression>, } "upsert": true|false } Aggregate Documents Endpoint Update Multiple Documents POST Base_URL/aggregate Endpoint Request Body POST Base_URL/updateMany Example: Request Body { Example: "dataSource": "<data source name>", { "database": "<database name>", "dataSource": "<data source name>", "collection": "<collection name>", "database": "<database name>", "pipeline": [<pipeline expression>, ...] "collection": "<collection name>", } "filter": <query filter>, "update": <update expression>, MongoDB Drivers "upsert": true|false The MongoDB Shell (mongosh) is great, } but generally you will need to use MongoDB in your application. To do this, Delete a Single Document MongoDB has many language drivers. Endpoint The language drivers allow you to interact POST Base_URL/deleteOne with your MongoDB database using the Request Body methods you've learned so far in Example: `mongosh` but directly in your application. { These are the current officially supported "dataSource": "<data source name>", drivers: "database": "<database name>", • C "collection": "<collection name>", • C++ "filter": <query filter> • C# } • Go MONGODB NOTES • Java • Node.js • PHP • Python • Ruby • Rust