MongoDB Connection Pool

https://stackoverflow.com/questions/41271707/java-mongodb-connection-pool

Advertisements

MongoDB Notes

Write Concern:
Acknowledge Write – 1 – DB Setting – False – Fast Response – Small error of missing write
Acknowledge Write – 1 – DB Setting – True – Slow Response – No Error
Unacknowledge Write-0 – Not waiting for server to respond.

In replicated environment, there are many more variables.

——-
Network Errors

use case 1: Write to DB, Due to network error, system didn’t responded. Data was written to disk
Option 1: Based on case, due to failure, try to write one more time.
Option 2: In sensitive case, read data to make sure that data was written and act acordingly.

——-
Replication

Availability
Fault Tolerance

To select Primary, we need to have odd number of servers.

—————

Types of Replica Set Nodes
1. Regular Node – Primary or Secondary
2. Arbiter Node – Only for voting purposes. No Data on it.
3. Delayed Node – It can’t become primary node. This is one hour late on updates with compare to other nodes.
4. Hidden Node – It can’t become primary node. Used for Analytics
All nodes can participate in election

——
Write Consistency

Always writes/reads goes to Primary
Application can read from secondary
Eventual consistency

During the time when failover is occurring, can writes successfully complete?
No

—————–

Sharding

mongos is router..takes care of distribution…
Sharding is used for horizontal scalability

—————–

MongoDB – Aggregation Expressions

sum
avg
min
max
push
addtoset
first
last
=================

Which of the following aggregation expressions must be used in conjunction with a sort to make any sense?
$first, $last
===================
use agg
db.products.aggregate([
{$group:
{
_id: {
“manufacturer”:”$manufacturer”
},
num_products:{$sum:”$price”}
}
}
])
================

mongoimport is independent command. Don’t run this from shell.
>mongoimport –db agg –collection zips –file zips.json

>mongo
>use agg
>db.zips.count()

use agg;
db.zips.aggregate([{“$group”:{“_id”:”$state”, “population”:{$sum:”$pop”}}}])

use agg;
db.zips.aggregate([{“$group”:{“_id”:”$state”, “average_pop”:{$avg:”$pop”}}}])

================
addToSet

use agg
db.products.aggregate([
{$group:
{
_id: {
“maker”:”$manufacturer”
},
categories:{$addToSet:”$category”}
}
}
])

{ “_id” : { “maker” : “Amazon” }, “categories” : [ “Tablets” ] }
{ “_id” : { “maker” : “Google” }, “categories” : [ “Tablets” ] }
{ “_id” : { “maker” : “Sony” }, “categories” : [ “Laptops” ] }
{ “_id” : { “maker” : “Samsung” }, “categories” : [ “Tablets”, “Cell Phones” ] }
{ “_id” : { “maker” : “Apple” }, “categories” : [ “Tablets”, “Laptops” ] }

================

Write an aggregation query that will return the postal codes that cover each city.

use agg;

db.zips.aggregate([{$group:{
“_id”: “$city”,
“postal_codes”: {“$addToSet”:”$_id”}
}}])
================
use agg
db.products.aggregate([
{$group:
{
_id: {
“maker”:”$manufacturer”
},
maxprice:{$max:”$price”}
}
}
])
================

MongoDB – Aggregation

db.stuff.insert({ “_id” : ObjectId(“50b26f9d80a78af03b5163c8”), “a” : 1, “b” : 1, “c” : 1 })
db.stuff.insert({ “_id” : ObjectId(“50b26fb480a78af03b5163c9”), “a” : 2, “b” : 2, “c” : 1 })
db.stuff.insert({ “_id” : ObjectId(“50b26fbf80a78af03b5163ca”), “a” : 3, “b” : 3, “c” : 1 })
db.stuff.insert({ “_id” : ObjectId(“50b26fcd80a78af03b5163cb”), “a” : 3, “b” : 3, “c” : 2 })
db.stuff.insert({ “_id” : ObjectId(“50b26fd380a78af03b5163cc”), “a” : 3, “b” : 5, “c” : 3 })

 

db.stuff.aggregate([{$group:{_id:'$c'}}])

{ "_id" : 3 }
{ "_id" : 2 }
{ "_id" : 1 }


---------------------

Compount Grouping

use agg
db.products.aggregate([
 {$group:
 {
 _id: {
 "manufacturer":"$manufacturer", 
 "category" : "$category"},
 num_products:{$sum:1}
 }
 }
])

use agg
db.products.aggregate([
 {$group:
 {
 _id: {
 "category" : "$category",
 "manufacturer":"$manufacturer"
 },
 num_products:{$sum:1}
 }
 }
])

---------------------

MongoDB – Aggregation

Aggregation Commands
aggregate
count
distinct
group
mapReduce

Aggregation Methods

https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/
Pipeline Aggregation Stages
$collStats
$project
$match
$redact
$limit
$skip
$unwind : Deconstructs an array field from the input documents to output a document for each element.
$group
$sample
$sort
$geoNear
$lookup
$out
$indexStats
$facet
$bucket
$bucketAuto
$sortByCount
$addFields
$replaceRoot
$count
$graphLookup

Boolean Aggregation Operators: $and, $or, $not

Set Operators: $setEquals, $setIntersection, $setUnion, $setDifference, $setIsSubset, $anyElementTrue, $allElementsTrue

Comparison Aggregation Operators: cmp, eq, gt, gte, lt, lte, ne

Arithmetic Aggregation Operators: abs, add, ceil, divide, exp, floor, ln, log, log10, mod, multiply, pow, sqrt, subtract, trunc

String Aggregation

Text Search Aggregation

Array Aggregation Operators

Aggregation Variable Operators

Aggregation Literal Operators

Date Aggregation Operators

Conditional Aggregation Operators

Group Accumulator Operators

Limitations:
By default 100 megabytes of RAM.
set the allowDiskUse option to true to overcome this issue.

MongoDB – Aggregation

use agg
db.products.drop()

db.products.insert({‘name’:’iPad 16GB Wifi’, ‘manufacturer’:”Apple”,
‘category’:’Tablets’,
‘price’:499.00})
db.products.insert({‘name’:’iPad 32GB Wifi’, ‘category’:’Tablets’,
‘manufacturer’:”Apple”,
‘price’:599.00})
db.products.insert({‘name’:’iPad 64GB Wifi’, ‘category’:’Tablets’,
‘manufacturer’:”Apple”,
‘price’:699.00})
db.products.insert({‘name’:’Galaxy S3′, ‘category’:’Cell Phones’,
‘manufacturer’:’Samsung’,
‘price’:563.99})
db.products.insert({‘name’:’Galaxy Tab 10′, ‘category’:’Tablets’,
‘manufacturer’:’Samsung’,
‘price’:450.99})
db.products.insert({‘name’:’Vaio’, ‘category’:’Laptops’,
‘manufacturer’:”Sony”,
‘price’:499.00})
db.products.insert({‘name’:’Macbook Air 13inch’, ‘category’:’Laptops’,
‘manufacturer’:”Apple”,
‘price’:499.00})
db.products.insert({‘name’:’Nexus 7′, ‘category’:’Tablets’,
‘manufacturer’:”Google”,
‘price’:199.00})
db.products.insert({‘name’:’Kindle Paper White’, ‘category’:’Tablets’,
‘manufacturer’:”Amazon”,
‘price’:129.00})
db.products.insert({‘name’:’Kindle Fire’, ‘category’:’Tablets’,
‘manufacturer’:”Amazon”,
‘price’:199.00})
————————-
use agg
db.products.aggregate([
{$group:
{
_id:”$manufacturer”,
num_products:{$sum:1}
}
}
])

{ “_id” : “Amazon”, “num_products” : 2 }
{ “_id” : “Google”, “num_products” : 1 }
{ “_id” : “Sony”, “num_products” : 1 }
{ “_id” : “Samsung”, “num_products” : 2 }
{ “_id” : “Apple”, “num_products” : 4 }
————————-
use agg
db.products.aggregate([
{$group:
{
_id:”$category”,
num_products:{$sum:1}
}
}
])

{ “_id” : “Laptops”, “num_products” : 2 }
{ “_id” : “Cell Phones”, “num_products” : 1 }
{ “_id” : “Tablets”, “num_products” : 7 }
————————-