This Repositry provides a comprehensive guide to help you get started with MongoDB, a popular NoSQL database. MongoDB is known for its flexibility, scalability, and ease of use, making it a preferred choice for many developers and organizations.
MongoDB is a document-oriented NoSQL database, designed to store, query, and process large amounts of unstructured or semi-structured data. It uses a flexible, JSON-like document format called BSON (Binary JSON) to represent data.
Key features of MongoDB:
- Schema-less: No predefined schema, allowing flexibility in data structure.
- High Performance: Supports indexing, sharding, and efficient query execution.
- Horizontal Scalability: Easily scales horizontally by adding more servers to the database.
Follow the official MongoDB installation guide to set up MongoDB on your system.
- Collections: Equivalent to tables in relational databases, collections store documents.
- Documents: BSON data format records that store data in key-value pairs.
- Fields: Key-value pairs within a document.
- Indexes: Improve query performance by providing a quick access path to the data.
// Insert a single document
db.users.insertOne({
name: "John Doe",
age: 25,
email: "john@example.com"
});
// Insert multiple documents
db.users.insertMany([
{ name: "Jane Doe", age: 30, email: "jane@example.com" },
{ name: "Bob Smith", age: 22, email: "bob@example.com" }
]);
// Find all documents in the users collection
db.users.find();
// Find a document by a specific field value
db.users.findOne({ name: "John Doe" });
// Update a single document
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 26 } }
);
// Update multiple documents
db.users.updateMany(
{ age: { $lt: 30 } },
{ $inc: { age: 1 } }
);
// Replace a single document
db.users.replaceOne(
{ name: "John Doe" },
{ name: "John Doe", age: 26, updated_at: new Date() }
);
// Delete a single document
db.users.deleteOne({ name: "Bob Smith" });
// Delete multiple documents
db.users.deleteMany({ age: { $gte: 30 } });
To interact with MongoDB, you can use the following commands in your terminal:
# Access the MongoDB shell
mongo
# Alternatively, you can use the MongoDB shell with improved features
mongosh
# Start the MongoDB server
mongod
MongoDB uses flexible, schema-less documents to store data. Documents in a collection can have different fields, and data doesn't need to be uniform.
Indexing is crucial for optimizing MongoDB queries. It helps in improving the speed of data retrieval operations. Ensure to create indexes based on the queries you frequently execute.
To connect your Node.js application to MongoDB using Mongoose, follow these steps:
-
Install Mongoose using npm:
npm install mongoose
-
In your Node.js code (e.g.,
app.js
), set up the connection to MongoDB:const mongoose = require('mongoose'); const dbUser = 'your_db_user'; const dbPassword = 'your_db_password'; mongoose .connect( `mongodb+srv://${dbUser}:${dbPassword}@cluster0.re3ha3x.mongodb.net/learn-mongodb`, { useNewUrlParser: true, useUnifiedTopology: true } ) .then(() => { console.log("Connected to MongoDB database!"); }) .catch((error) => { console.error("Connection failed!", error); });
Replace your_db_user
and your_db_password
with your MongoDB Atlas database username and password. Ensure that you have the necessary permissions for connecting to the cluster.
Make sure to include the error handling to log any connection issues for better debugging.
This setup assumes you are using MongoDB Atlas, the cloud-based MongoDB service. Adjust the connection string accordingly if you are using a different MongoDB deployment.
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com",
"age": 25,
"gender": "Male",
"dateOfBirth": "1997-01-15",
"address": {
"street": "123 Main St",
"city": "Cityville",
"state": "CA",
"zipCode": "12345"
},
"phoneNumber": "123-456-7890",
"isActive": true
}
View all databases
show dbs
Create a new or switch databases
use dbName
View current Database
db
Delete Database
db.dropDatabase()
Show Collections
show collections
Create a collection named 'comments’
db.createCollection('data')
Drop a collection named 'comments’
db.comments.drop()
Show all Rows in a Collection
db.comments.find()
Show all Rows in a Collection (Prettified)
db.comments.find().pretty()
Find the first row matching the object
db.comments.findOne({name: 'Manthan'})
Insert One Row
db.comments.insert({
'firstName': 'Manthan',
'lastName': 'Ank'
})
Insert many Rows
db.comments.insertMany([{'firstName': 'Manthan', 'lastName': 'Ank'},{'firstName': 'Gagan','lastName': 'BA'}])
Search in a MongoDB Database
db.comments.find({lang:'JavaScript'})
Limit the number of rows in output
db.comments.find().limit(3)
Count the number of rows in the output
db.comments.find().count()
Update a row
db.comments.updateOne({name: 'Manthan'},{$set: {'name': 'Manthan','lang': 'JavaScript','mem_since': 1}},{upsert: true})
MongoDB Increment Operator
db.comments.update({name: 'Manthan'},{$inc:{mem_since: 2}})
MongoDB Rename Operator
db.comments.update({name: 'Manthan'},{$rename:{mem_since: 'member'}})
Delete Row
db.comments.remove({name: 'Manthan'})
Less than/Greater than/ Less than or Eq/Greater than or Eq
db.comments.find({member_since: {$lt: 90}})
db.comments.find({member_since: {$lte: 90}})
db.comments.find({member_since: {$gt: 90}})
db.comments.find({member_since: {$gte: 90}})
Here are a few suggestions and additions to enhance the guide:
MongoDB's Aggregation Framework is a powerful tool for data transformation and analysis. You can include examples of aggregation pipeline stages like $match
, $group
, $project
, and others.
db.collection.aggregate([
{ $match: { field: value } },
{ $group: { _id: "$field", count: { $sum: 1 } } },
{ $project: { _id: 0, field: "$_id", count: 1 } }
])
MongoDB supports full-text search capabilities. You can demonstrate how to perform text searches on a text index.
db.collection.createIndex({ fieldName: "text" })
db.collection.find({ $text: { $search: "searchQuery" } })
MongoDB has built-in support for geospatial queries. You can showcase how to query documents based on their geographical location.
db.collection.createIndex({ locationField: "2dsphere" })
db.collection.find({
locationField: {
$near: {
$geometry: { type: "Point", coordinates: [longitude, latitude] },
$maxDistance: 1000 // in meters
}
}
})
If you are using MongoDB version 4.0 or above, you can include examples of transactions for handling multiple operations atomically.
session = db.getMongo().startSession()
session.startTransaction()
try {
// Perform multiple operations
db.collection1.updateOne({ field: value1 }, { $set: { updateField1: newValue1 } })
db.collection2.updateOne({ field: value2 }, { $set: { updateField2: newValue2 } })
session.commitTransaction()
} catch (error) {
print("Transaction failed. Aborting...")
session.abortTransaction()
}
finally {
session.endSession()
}
MongoDB 3.6 and later versions support JSON Schema validation. You can provide examples of how to enforce a schema on a collection.
db.createCollection("validatedCollection", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["field1", "field2"],
properties: {
field1: { bsonType: "string" },
field2: { bsonType: "int" }
}
}
}
})
Briefly touch upon MongoDB security practices, such as authentication, authorization, and connection security.
# Creating a user with readWrite privileges
use admin
db.createUser({
user: "username",
pwd: "password",
roles: [{ role: "readWrite", db: "databaseName" }]
})
Discover MongoDB Atlas, the cloud-based database service, and learn how to deploy, manage, and scale MongoDB clusters.
If you find any issues or have suggestions for improvement, feel free to contribute. Follow the contribution guidelines for details.
This MongoDB tutorial is licensed under the MIT License. Feel free to use, modify, and distribute this tutorial for educational purposes.