L48 - MongoDB
L48 - MongoDB
An overview of NoSQL
COS216
AVINASH SINGH
DEPARTMENT OF COMPUTER SCIENCE
UNIVERSITY OF PRETORIA
Big Data - Overview
RDBMS MongoDB
Database Database
Table Collection
Row Document
Column Field
Primary Key Primary Key
MongoDB - Documents
Name Description
mongod Daemon/server for MongoDB that has to run in the
background
mongo The client used to connect to the server and execute
queries
mongoimport Import JSON documents into MongoDB
mongoexport Export JSON documents from MongoDB
MongoDB - Connection
Type Description
String UTF-8 strings
Boolean True/False values
Integer 32bit or 64bit integers
Double Floating points
Arrays Arrays of any of the other types
Timestam Unix timestamp with ordinal
ps operations
Date Date and times
Object Embedded documents
Code JavaScript code
Regex Regular expressions
MongoDB – GRUD Operations
db.myCollection.insert([
{name : "Bitcoin", price : 8521.32},
{name : "Ethereum", price : 625.32}
]);
MongoDB - Find
Other operators are available: less than ($lt), less than or equal ($lte), greater than
($gt), greater than or equal ($gte), not equal ($ne)
MongoDB - Find
db.myCollection.find({$or : [
{name : "Bitcoin"},
{name : "Ethereum"}
]});
MongoDB - Sort
db.myCollection.update({name : "Bitcoin"},
{$set : {price : 8652.23}},
{multi : false});
MongoDB - Remove
db.myCollection.remove({name : "Bitcoin"});
MongoDB - Aggregation
.aggregate([
{ $match : <document criteria> }, // Limits data before grouping
{ $group : <group specification> }, // Grouping data
{ $match : <group criteria> }, // Limits results after grouping
{ $sort : <sort specification> }, // Sorts grouped data
{ $out : <collection> }, // The results are inserted into a collection
]) ;
MongoDB - Aggregation
Result
db.myCollection.aggregate([
{
$group : {_cap : {$sum : "$marketcap"}}
}
])
Result
{"_cap" : "390776273498"}
MongoDB - Aggregation
Example: find all coins above $5000 and sort them in descending order according to
price
db.myCollection.aggregate([
{$group : {_id : "$name", _price : "$price"}},
{$match : {_price : {$gt : 5000}}},
{$sort : {_price : -1}},
{$out : "CoinResults"}}
]) ;