readConcern
readPreference
writeConcern
db.getCollection('insert collection name here!').find({}).toArray()
Note: could use .hasNext()/.next()
to iterate
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!
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.
// use('sample_analytics')
db // (default? test)
use('new db')
db.getMongo().getDBs()
db.dropDatabase()
db.createCollection('test_collection')
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'],
},
},
},
},
])