Mongo DB as part of MERN Stack

Why do people use Mango DB?

MongoDB does not require a predefined schema, this allow users to store documents with varying structures within the same collection. This flexibility eliminates the need for complex JOIN operations, as related data can be embedded directly within a single document.

As a result, MongoDB can efficiently produce aggregated results without incurring the overhead typically associated with joins in relational databases. Mongo DB supports automatic shading to scale out horizontally.

On the other hand, PostgreSQL is a relational database that uses rows and columns with a defined schema. It offers native support for complex joins and advanced relational operations.

Choosing between MongoDB and PostgreSQL should depend on your specific use case. If your data is well-structured and relational, PostgreSQL is likely the better choice. However, if your data is more flexible or evolving, MongoDB may be a better fit.

To get the most out of MongoDB, it’s important to start by learning its querying methods. 🧐

updateOne
{$unset: {age: ""}}
{$push: {hobbies: "swimming"}}
{$pull: {hobbies: "Bowling"}}

updateMany({address: {exists: true}}, {$unset: {address: ""}})


// Stage 1: Filter pizza order documents by pizza size
db.orders.aggregate( [
   {
      $match: { size: "medium" }
   },
// Stage 2: Group remaining documents by pizza name and calculate total quantity
   {
      $group: { _id: "$name", totalQuantity: { $sum: "$quantity" } }
   }
] )

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *