All about MongoDB

author

Aneh Thakur

. 3 min read

Follow

What is MongoDB?

MongoDB is a cross-platform document-oriented database program. It is classified as a NoSQL database program, meaning that it does not use the traditional SQL relational database management system (RDBMS) model. Instead, it uses JSON-like documents with optional schemas. MongoDB is used to store large amounts of data in a flexible, JSON-like format called BSON (Binary JSON). It is designed to scale horizontally across multiple servers, and to support modern, high-performance applications that require fast access to large amounts of data.

Some key features of MongoDB include:

  • Scalability: MongoDB is designed to scale horizontally across multiple servers, using a technique called sharding. This allows it to handle very large data sets and high levels of concurrency without sacrificing performance.
  • Flexibility: MongoDB uses a flexible, JSON-like document data model, which allows you to store data in a way that is more natural for your application. This makes it easier to work with complex data structures and to change the data model as your application evolves.
  • High performance: MongoDB is designed to be fast, with support for efficient indexing, sharding, and in-memory caching. It can handle high levels of concurrency and large numbers of read and write operations per second.

MongoDB is used by many organizations, including Fortune 500 companies and startups, in a wide range of industries, including finance, e-commerce, healthcare, and media. It is an open-source database program, with a large and active community of users and developers.

Difference between MongoDB and Mysql

MongoDB and MySQL are both popular database management systems, but they have some significant differences. Here are some key points to consider when comparing the two:

  • Data model: One of the main differences between MongoDB and MySQL is the way they store data. MySQL uses a traditional relational database model, which stores data in tables with rows and columns. MongoDB, on the other hand, uses a document-oriented model, which stores data in flexible, JSON-like documents. This allows MongoDB to handle complex data structures and relationships more easily, but it can be more difficult to work with if you are used to the relational model.
  • Scalability: MongoDB is designed to scale horizontally across multiple servers, using a technique called sharding. This makes it well-suited for handling very large data sets and high levels of concurrency. MySQL, on the other hand, is generally not as scalable as MongoDB, although it does offer some features for scaling up, such as partitioning and replication.
  • Querying: Both MongoDB and MySQL support powerful querying capabilities, but the syntax and capabilities of their query languages are quite different. MySQL uses SQL (Structured Query Language), which is a standard language for accessing and manipulating relational databases. MongoDB uses a query language called the MongoDB Query Language (MQL), which is more flexible and allows you to work with documents in a more natural way.
  • Use cases: Both MongoDB and MySQL are used in a wide range of applications, but they are generally better suited to different types of use cases. MongoDB is often used for modern, high-performance applications that need to store and retrieve large amounts of data quickly, such as real-time analytics or content management systems. MySQL is often used for more traditional, structured data storage and management tasks, such as accounting or inventory management.

Overall, the choice between MongoDB and MySQL will depend on your specific needs and requirements. Both are powerful and widely used database management systems, but they have different strengths and are better suited to different types of applications.

CURD operation example in MongoDB

CURD stands for "Create, Update, Read, and Delete," which are the four basic operations that most database management systems support. Here is an example of how you might perform these operations using the MongoDB query language:

  • Create: To insert a new document into a collection in MongoDB, you can use the insertOne or insertMany method. For example:
db.users.insertOne({
  name: "Alice",
  age: 25,
  hobbies: ["reading", "running"]
});


db.users.insertMany([
  {
    name: "Bob",
    age: 30,
    hobbies: ["guitar", "cooking"]
  },
  {
    name: "Charlie",
    age: 35,
    hobbies: ["hiking", "photography"]
  }
]);

  • Read: To retrieve documents from a collection in MongoDB, you can use the find method. For example:
db.users.find({age: {$gt: 30}}); // Find all users with age greater than 30
db.users.find({hobbies: "running"}); // Find all users with "running" as a hobby

  • Update: To update an existing document in a collection in MongoDB, you can use the updateOne or updateMany method. For example:
db.users.updateOne({name: "Alice"}, {$set: {age: 26}}); // Increase Alice's age by 1
db.users.updateMany({hobbies: "guitar"}, {$push: {hobbies: "singing"}}); // Add "singing" as a hobby for all users who play guitar

  • Delete: To delete a document from a collection in MongoDB, you can use the deleteOne or deleteMany method. For example:
db.users.deleteOne({name: "Charlie"}); // Delete the user with name "Charlie"
db.users.deleteMany({age: {$lt: 30}}); // Delete all users with age less than 30

These are just a few examples of the types of operations you can perform with MongoDB. The query language is very flexible and allows you to perform a wide range of operations on your data.


More Stories from Trinitytuts Tech

Monetize react native app with Google Admob

Aneh Thakur.2 min read
Monetize react native app with Google Admob

Deploying React & Node.js App using GitHub Actions CI/CD

Aneh Thakur.5 min read
Deploying React & Node.js App using GitHub Actions CI/CD

What is DevOps?

Aneh Thakur.1 min read
What is DevOps?

Different between find(), filter() and map() in javascript

Aneh Thakur.2 min read

What is React context API?

Aneh Thakur.3 min read
What is React context API?