Skip to content

Instantly share code, notes, and snippets.

@vhbui02
Last active May 18, 2023 03:53
Show Gist options
  • Save vhbui02/95406c545cc1f3acf1a3a46340f6e8ee to your computer and use it in GitHub Desktop.
Save vhbui02/95406c545cc1f3acf1a3a46340f6e8ee to your computer and use it in GitHub Desktop.
[MongoDB General] takes weeks to months to learn, i've managed in 3 days #db #mongodb

Research later

  • readConcern
  • readPreference
  • writeConcern

Return full result set in VSCode playground (default in playground is 20)

db.getCollection('insert collection name here!').find({}).toArray()

Note: could use .hasNext()/.next() to iterate

No need pretty() if you're using VSCode Playground, maybe yes in MongoDB Compass

NoSQL over SQL, 1001 why and answer?

Denormalized data model (using embedded documents and array) are something that RDBMS do not have. It can't put a table inside a table or make a column holds multiple value, instead it have multiple table and foreign key to describe the 'relation' to make a logical pseudo 'table-in-table' and 'multiple value in 1 column'.

MongoDB is different. They don't make multiple individual collections that have multiple individual documents that related to each other like in RDBMS.

Just simply, embedded it. Now a single-document transaction can do the work of multi-document transaction.

Single-document transaction can be atomic. But Atomicity is not achievable on multi document operation in MongoDB. There for you need to construct a multi-document transaction, which is something very hard to do if you dont have experience!

But, sometimes you would need to use RDBMS

Imagine, you decided to put OrderHistory as embedded document to Inventory. That is not possible since:

  • An OrderHistory document contain multiple bought Inventory item, how can it happens to be Embedded Document inside only 1 Inventory document?
  • Now I want to create a Collection called TotalRevenue by Month, how can I pull the Embedded Document OrderHistory inside Inventory and put it inside TotalRevenue collection?

Best: 3 Collection: OrderHistory and Inventory, and JOIN them.

But, in a 'logical container relationship', such as, a blog post contains its comment

Relational databases are ideal for applications that require data consistency, structured data, and complex relationships between data entities. For example, a banking application that needs to store customer data, transaction records, and account information would benefit from using a relational database. When to use a Non-Relational Database?

Non-relational databases are ideal for applications that require scalability, flexibility, and handling unstructured data. For example, a social media platform that needs to store user-generated content, such as photos, videos, and posts, would benefit from using a non-relational database.

Currently used database?

// use('sample_analytics')
db // (default? test)

C db if not exist

use('new db')

R all databases

db.getMongo().getDBs()

D database

db.dropDatabase()

C collection

db.createCollection('test_collection')

Aggregation

use('sample_supplies')
db.getCollection('sales').aggregate([
   // Find all of the sales that occurred in 2014.
   {
      $match: {
         saleDate: {
            $gte: new Date('2014-01-01'),
            $lt: new Date('2015-01-01'),
         },
      },
   },
   {
      $limit: 1,
   },
   {
      $unwind: '$items',
   },
   {
      $project: {
         itemName: '$items.name',
         itemPrice: '$items.price',
         itemQuantity: '$items.quantity',
         // items: {
         //    name: 1,
         //    price: 1,
         //    quantity: 1,
         // },
      },
   },
   // Group the total sales for each product.
   {
      $group: {
         _id: '$_id',
         totalSaleAmount: {
            $sum: {
               $multiply: ['$itemPrice', '$itemQuantity'],
            },
         },
      },
   },
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment