MongoDB Basics
MongoDB Basics
MongoDB Introduction
MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
Advantages of MongoDB
- Flexible schema - you don’t need to declare a schema to use MongoDB. This is useful when it comes to storing unstructed data.
- Scalability - MongoDB is a distributed database, so it can be horizontally scale with sharding.
- Powerful querying and analytics - MongoDB allows users to easily query the database and rarely use joins and transactions.
When to avoid MongoDB
- Multi Object Transaction - MongoDB only allows ACID transaction for a single document.
- Relational data - If the dataset has many one-to-many or many-to-many relations and needs joins, you should use traditional SQL database instead.
Use cases
- Personalization - track users search and activity to create personalized online experience.
- Product Catelog, asset managements - we can store product or asset data and their metadata in MongoDB because of its flexible schema and powerful query capability.
- IoT application - MongoDB is horizontally scalability makes it a better fit for data generated by IoT sensors. MongoDB also offers Time Series Collections for IoT applications.
Document and Collection
MongoDB is a document database so it stores records as Document. MongoDB stores documents as BSON. It is a JSON like document format.
Documents are organized as Collection. Collection is similar to Table in relational database.
Running MongoDB as Docker Container
Install using Docker
1 | docker pull mongo |
Running mongo container
1 | docker run --name some-mongo -d -p 27017:27017 mongo |
Running mongo container with username and password
1 | docker run -d --name some-mongo \ |
Use docker exec
command to get shell access
1 | docker exec -it [container-id] bash |
You can now run mongodb
command to start MongoDB Shell.
1 | mongodb |
To connect to MongoDB using username and password
1 | mongodb -u myusername -p mypassword |
The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 16.x REPL environment for interacting with MongoDB deployments. You can use the MongoDB Shell to test queries and operations directly with your database.
MongoDB Compass is the GUI you can use to interact with MongoDB.
Database Commands
1 | # show all databases in the server |
Collection Commands
1 | # Show all collections |
Document Commands
Insert
1 | # db.collection.insertOne() inserts a single document into a collection. |
Query
1 | # get all documents in a collection |
$gt
, $and
, $text
are all Query Selectors. specifically, $gt
is comparison operator and $and
is logical operator.
Update
1 | # Modifies a single document in a collection. |
Delete
1 | # delete a single document that match the condition that price is greater than |
Index Commands
1 | # create index with name price_idx |
createIndex can take a list of options. see Options for the complete list of options.