MongoDB is a NoSQL database, and its structure is different from traditional relational databases like MySQL or PostgreSQL. Instead of using tables, rows, and columns, MongoDB organizes data into databases and collections, which are more flexible and scalable.
Here’s a breakdown of databases and collections in MongoDB, along with how to work with them.
A database in MongoDB is a container for collections. Each database has its own set of collections, indexes, and data. A MongoDB instance can contain multiple databases.
use
command. MongoDB will automatically create the database when you insert data into it.Example:javascriptCopy codeuse myDatabase; // Switch to or create 'myDatabase'
show databases;
dropDatabase()
method. This removes the database and all its collections permanently.Example:javascriptCopy codedb.dropDatabase();
db;
A collection is a group of MongoDB documents. Collections are the MongoDB equivalent of tables in relational databases. However, unlike relational tables, MongoDB collections do not require a schema, meaning each document can have a different structure.
createCollection()
method.Example:javascriptCopy codedb.createCollection("users"); // Explicitly create a collection named "users"
show collections;
drop()
method. This deletes the collection and all of its documents.Example:javascriptCopy codedb.users.drop(); // Drop the "users" collection
db.users; // Access the "users" collection
A document is a single record in a collection. It is represented in BSON (Binary JSON) format, which is similar to JSON but allows for more data types like ObjectId
, Date
, and binary data.
{ "_id": ObjectId("60d5f89d75c0fe3f488a3b0b"), "name": "John Doe", "email": "john.doe@example.com", "age": 29, "address": { "street": "123 Main St", "city": "Springfield" }, "tags": ["developer", "mongodb"] }
_id
field, which serves as the primary key. If you don’t specify an _id
, MongoDB will automatically generate one using ObjectId
.use myDatabase;
db.users.insertOne({ name: "Alice", email: "alice@example.com", age: 30 });
db.users.insertMany([ { name: "Bob", email: "bob@example.com", age: 35 }, { name: "Charlie", email: "charlie@example.com", age: 28 } ]);
db.users.find(); // Fetch all documents in the 'users' collection
db.users.find({ name: "Alice" }); // Find users named "Alice"
db.users.findOne({ _id: ObjectId("60d5f89d75c0fe3f488a3b0b") });
db.users.updateOne( { name: "Alice" }, { $set: { age: 31 } } );
db.users.updateMany( { age: { $gte: 30 } }, { $inc: { age: 1 } } );
db.users.deleteOne({ name: "Alice" });
db.users.deleteMany({ age: { $lt: 30 } });
Indexes are used to improve the performance of queries by allowing MongoDB to find data more efficiently. Indexes are created on one or more fields of a collection.
db.users.createIndex({ email: 1 }); // Ascending index on the 'email' field
db.users.createIndex({ name: 1, age: -1 }); // Compound index on 'name' and 'age'
db.users.getIndexes();
db.users.dropIndex("email_1"); // Drop the index on 'email'
_
) characters in names. Database and collection names:
.
) or dollar sign ($
) characters.db.createCollection()
.MongoDB uses a flexible and scalable structure based on databases, collections, and documents. Each database can hold multiple collections, and each collection holds documents. This NoSQL approach provides high flexibility for handling dynamic data with various data types and structures.
Understanding how to work with databases and collections is fundamental when designing applications and structuring data in MongoDB.