33 Questions
Which one of the following is a characteristic of RDBMS?
Enforced structured data model based on tables with predefined schemas
What is the problem with RDBMS?
Fixed schema
Which of the following is not a feature of RDBMS?
Dynamic fields on demand
Which method in MongoDB is used to insert multiple documents at once?
insertMany()
Which method in MongoDB is used to select data from a collection and accepts a query object?
find()
Which parameter is used to specify which fields to include in the results when using the find() method in MongoDB?
projection
Which method in MongoDB is used to update an existing document and update the first document that matches the query provided?
updateOne()
Which method can be used to query a collection in MongoDB?
find()
What is the syntax for specifying query filters in MongoDB Query Language (MQL)?
{ 'key': { '$selector': expression } }
Which query operator is used to match a value greater than another value in MongoDB?
$gt
Which update operator is used to increment the value of a field in MongoDB?
$inc
Which type of scaling relies on adding more resources to a single server when data size grows?
Vertical scaling
What does NoSQL often stand for?
Not Only SQL
Which type of database system allows dynamic data structures and horizontal scalability?
Document-oriented
What is the main difference between SQL and NoSQL databases?
SQL databases have fixed schema, while NoSQL databases have dynamic schema
Which method is used to create a new user in the MongoDB Atlas dashboard?
Database Access
Where can you find the official instructions to install mongosh?
What does CRUD operations stand for in MongoDB?
Create, Read, Update, Delete
How can you see all available databases in mongosh?
Type 'show dbs' in the terminal
Which programming languages are officially supported by MongoDB?
Go
What is the recommended way to load sample data into a MongoDB Atlas database?
Click the ellipsis and select 'Load Sample Dataset'
What package do you need to install in your Node.js project to use MongoDB?
mongodb
What method can be used to find the first document in a MongoDB collection using the MongoDB Node.js language driver?
collection.findOne()
Node.js is a _______ that allows you to run JavaScript on the server side.
runtime environment
To use MongoDB with Node.js, you will need to install the _______ package in your Node.js project.
mongodb
To connect to a MongoDB Atlas database, you need to get the _______ from the Atlas dashboard.
connection string
To use MongoDB with Node.js, you will need to install the _______ package in your Node.js project.
mongodb
const uri
mongodb+srv://:@.mongodb.net/
Let's add to our index.js file. const client = new MongoClient(uri); async function run() { try { await client.connect(); const db = client.db('sample_mflix'); const collection = db.collection('movies'); // Find the first document in the collection const first = await collection.findOne(); console.log(first); } finally { // Close the database connection when finished or an error occurs await client.close(); } } run().catch(console.error);
Connecting to MongoDB
Just as we did using mongosh, we can use the MongoDB Node.js language driver to create, read, update, and delete in the database. Expanding on the previous example, we can replace the collection.findOne() with find(), insertOne(), insertMany(), u pdateOne(), updateMany(), deleteOne(), or deleteMany(). Give some of those a try.
CRUD operations
What is the purpose of the MongoDB Atlas database in this tutorial?
To load and interact with sample data
What command is used to install the mongodb package in a Node.js project?
npm install mongodb
What method is used to find the first document in a MongoDB collection using the MongoDB Node.js language driver?
collection.findOne()
Study Notes
RDBMS (Relational Database Management System)
- Stands for "Relational Database Management System"
- Enforced structured data model based on tables with predefined schemas
- Each table in a database schema has a unique name and defines the structure of the data it stores, including:
- Data types of the data columns
- Relationships between tables
- Constraints
- Primary keys
- Foreign keys
- Fixed schema
- Need to alter the table structure if the schema changes, which is complex and time-consuming
- Cannot have dynamic fields on demand, i.e., add a field for specific records
- Scale up (Vertical scaling): relies on vertical scaling, adding more resources to a single server, which is expensive and not designed to be distributed
- Use centralized architecture
NoSQL Database
- Often interpreted as "Not Only SQL"
- A class of schema-less database system
- Allows dynamic data structures
- Horizontal scalability
- Distributes data across multiple servers to handle large volumes of data
- Supports different types of data models
NoSQL Types
- Key-value stores
- Graph database
- Document-oriented
- Column family
MongoDB
- An open-source, document-oriented database designed with scalability and developer agility in mind
- Stores data in JSON-like documents with dynamic schemas (schema-free, schema-less)
- Features:
- Document-oriented database
- Built-in horizontal scalability
- High performance and indexing
- Rich queries and data aggregations
- Integration with various application frameworks
- Large ecosystem and community
MongoDB vs RDBMS
- RDBMS has a fixed schema, while MongoDB has a dynamic schema
- RDBMS is best suited for complex queries, while MongoDB is not good for complex queries
- RDBMS is vertically scalable, while MongoDB is horizontally scalable
MongoDB Atlas
- A cloud database platform
- Easier to use than hosting a local database
- Allows for deployment of a database by setting up a free "Shared Cluster" and choosing a preferred cloud provider and region
MongoDB CRUD Operations
- Create: create or insert operations add new documents to a collection
- Read: retrieve documents from a collection
- Update: modify existing documents in a collection
- Delete: remove documents from a collection
MongoDB mongosh
- A way to interact with a MongoDB database
- Can be used to create a database, collection, and documents
- Can perform CRUD operations
- Can be used to query and manipulate data
Creating a Database and Collection
- Use the
use
command to create or switch to a database - Use the
createCollection()
method to create a collection - Alternatively, a collection can be created during the insert process
Inserting Documents
- Use the
insertOne()
method to insert a single document - Use the
insertMany()
method to insert multiple documents - Documents can be JSON objects
Querying Data
- Use the
find()
method to select data from a collection - Use the
findOne()
method to select only one document - Can use a query object to filter data
- Can use a projection object to specify which fields to include in the results
Updating Documents
- Use the
updateOne()
method to update the first document that matches the query - Use the
updateMany()
method to update all documents that match the query - Can use the
upsert
option to insert a document if it is not found
Deleting Documents
- Use the
deleteOne()
method to delete the first document that matches the query - Use the
deleteMany()
method to delete all documents that match the query - Can use the
drop()
method to remove a collection or view from the database### MongoDB Basics - MongoDB is a NoSQL database that allows for dynamic schema changes.
- It uses a document-oriented data model, where each document is a JSON-like object with a dynamic schema.
MongoDB Query Language (MQL)
- MQL is based on a JSON-like syntax.
- Valid query expression formats:
- { "key": value, … }
- { "key": { "$selector": expression, … }, … }
- { "$selector": expression, … }
- { "$selector": [expression, …], … }
- Empty {} matches all documents.
- Each key-value pair in the query must be satisfied.
Query Selectors
- Comparison selectors:
- $eq: values are equal
- $ne: values are not equal
- $gt: value is greater than another value
- $gte: value is greater than or equal to another value
- $lt: value is less than another value
- $lte: value is less than or equal to another value
- $in: value is matched within an array
- Logical selectors:
- $and: returns documents where both queries match
- $or: returns documents where either query matches
- $nor: returns documents where both queries fail to match
- $not: returns documents where the query does not match
- Evaluation selectors:
- $regex: allows the use of regular expressions when evaluating field values
- $text: performs a text search
- $where: uses a JavaScript expression to match documents
Update Operators
- Fields:
- $currentDate: sets the field value to the current date
- $inc: increments the field value
- $rename: renames the field
- $set: sets the value of a field
- $unset: removes the field from the document
- Arrays:
- $addToSet: adds distinct elements to an array
- $pop: removes the first or last element of an array
- $pull: removes all elements from an array that match the query
- $push: adds an element to an array
MongoDB Node.js Driver
- To use MongoDB with Node.js, you need to install the mongodb package.
- Import the MongoClient class:
import { MongoClient } from 'mongodb';
- Connect to the database using the connection string:
const client = new MongoClient(uri);
- Create a database and collection:
const db = client.db('sample_mflix'); const collection = db.collection('movies');
CRUD Operations
- Create: insertOne() and insertMany() methods add new documents to a collection.
- Read: retrieve documents from a collection.
- Update: modify existing documents in a collection.
- Delete: remove documents from a collection.
MongoDB Atlas
- Create a cluster to deploy a database.
- Create a user and add IP addresses to the list of allowed IP addresses.
- Connect to the database using the MongoDB Shell (mongosh).
- Use the connection string provided by the MongoDB Atlas dashboard.
MongoDB Shell (mongosh)
- Install mongosh on your operating system.
- Verify the installation by typing
mongosh
in the terminal. - Connect to the database using the connection string.
- Use the
db
command to see which database you are using. - Use the
show dbs
command to see all available databases. - Use the
use
command to change or create a new database.### MongoDB Basics - MongoDB is a NoSQL database that allows for flexible schema design and scalable data storage.
- MongoDB provides a MongoDB Shell (mongosh) for interacting with the database.
Inserting Data
-
db.posts.insertOne()
method is used to insert a single document into a collection. -
db.posts.insertMany()
method is used to insert multiple documents into a collection. - If a collection does not exist, MongoDB will create it automatically when inserting documents.
Finding Data
-
find()
method is used to retrieve data from a collection. -
findOne()
method is used to retrieve a single document from a collection. - Both methods accept a query object to filter results.
- Projection is used to specify which fields to include in the results.
Updating Data
-
updateOne()
method is used to update a single document in a collection. -
updateMany()
method is used to update multiple documents in a collection. - The
$set
operator is used to update a field in a document. - The
$inc
operator is used to increment a field in a document.
Deleting Data
-
deleteOne()
method is used to delete a single document from a collection. -
deleteMany()
method is used to delete multiple documents from a collection. -
drop()
method is used to delete a collection or view from the database.
Query Language
- MongoDB Query Language (MQL) is used for querying and manipulating data.
- MQL is based on a JSON-like syntax.
- Queries can be used to filter data using various operators, such as
$gt
,$lt
,$eq
, and$in
.
Query Operators
- Comparison operators:
$eq
,$ne
,$gt
,$gte
,$lt
,$lte
, and$in
. - Logical operators:
$and
,$or
, and$nor
. - Evaluation operators:
$regex
and$text
.
MongoDB Node.js Driver
- The MongoDB Node.js driver is used to interact with a MongoDB database in a Node.js application.
- The driver provides methods for inserting, finding, updating, and deleting data.
- The driver can be used to connect to a MongoDB Atlas database.
CRUD Operations
- Create:
insertOne()
andinsertMany()
methods. - Read:
find()
andfindOne()
methods. - Update:
updateOne()
andupdateMany()
methods. - Delete:
deleteOne()
anddeleteMany()
methods.
Test your knowledge on MongoDB and its collection creation and document insertion features in this quiz. Learn about automatic collection creation and how to insert multiple documents at once.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free