What is MongoDB?
MongoDB is a database. More specifically, a document-oriented one. We call it a NoSQL database and it's designed for scalability.
The data model is based on collections and documents. This means that a collection is a grouping of documents. A important concept is that MongoDB allows documents in the same collection to have different fields.
Key Concepts
- A record in a collection is typically represented in BSON format.
- A collection is a grouping of MongoDB documents, as said before. They are analogous to tables in relational databases.
- A database is a container for collections, and MongoDB can host multiple databases on the same server.
NoSQL vs. Relational Databases
It is very important to know the differences between MongoDB and MySQL, for example. A relational database (RDBMS) has structured data with a fixed schema. You need to follow the predefined columns and data types.
In a NoSQL database, for example, you can have flexibilty, considering you have no obligatory schema. Changes to the schema can be complex, so if you want flexibility, NoSQL databases are definitely better.
Relational databases uses SQL language for querying, while in a NoSQL database the languages vary. MongoDB uses a JavaScript-based query language.
Relational databases are well-suited for complex queries and transactions, and ACID (Atomicity, Consistency, Isolation, Durability) properties are essential. NoSQL databases, on the other hand, are suited for large amounts of unstructured or semi-structured data, with a high read and write scalability.
Relational Databases examples:
- MySQL, PostgreSQL, Oracle, SQL Server. NoSQL Databases examples:
- MongoDB, Cassandra, Couchbase, Redis.
MongoDB Basics
Now that you know about the key features, here is a MongoDB basic guide created by ChatGPT.
- Installation:
- Download MongoDB: MongoDB Download Center
- Install and follow instructions based on your operating system.
- Starting MongoDB:
- Start MongoDB server:
mongod
- Connect to MongoDB:
mongo
- Start MongoDB server:
Database Operations:
- Create/Use Database:
- Create a new database:
use mydatabase
- Create a new database:
- Collection Operations:
- Create a collection:
db.createCollection("mycollection")
- Show collections:
show collections
- Drop a collection:
db.mycollection.drop()
- Create a collection:
Document Operations:
- Insert Documents:
- Insert a single document:
db.mycollection.insertOne({ key: "value" })
- Insert multiple documents:
db.mycollection.insertMany([{...}, {...}])
- Insert a single document:
- Query Documents:
- Find all documents:
db.mycollection.find()
- Find with a condition:
db.mycollection.find({ key: "value" })
- Find all documents:
- Update Documents:
- Update a document:
db.mycollection.updateOne({ key: "value" }, { $set: { newKey: "newValue" } })
- Update multiple documents:
db.mycollection.updateMany({ key: "value" }, { $set: { newKey: "newValue" } })
- Update a document:
- Delete Documents:
- Delete a document:
db.mycollection.deleteOne({ key: "value" })
- Delete multiple documents:
db.mycollection.deleteMany({ key: "value" })
- Delete a document:
Indexing:
- Create Index:
- Create a single-field index:
db.mycollection.createIndex({ key: 1 })
- Create a single-field index:
Aggregation:
- Aggregation Pipeline:
- Use the aggregation pipeline for complex queries:
db.mycollection.aggregate([{ $match: { key: "value" } }, { $group: { _id: "$key", count: { $sum: 1 } } }])
- Use the aggregation pipeline for complex queries:
Miscellaneous:
Exit MongoDB Shell:
- Exit the MongoDB shell:
quit()
orexit
- Exit the MongoDB shell:
MongoDB Atlas:
- MongoDB's cloud service for hosting MongoDB databases: MongoDB Atlas
Remember to replace "mydatabase" and "mycollection" with your actual database and collection names. This cheat sheet covers basic operations; MongoDB has many more advanced features and options. Refer to the MongoDB documentation for detailed information.