Académique Documents
Professionnel Documents
Culture Documents
Tutorials
Joshi
Levels of Data
1. Database
2. Collection
3. Document
Common Mistakes
Dont put the key in quotation:
ObjectId("53baf7f6cbda7100842d810d")
Use the $ Sign before the aggregate commands but
dont use quotation if you are at RoboMongo Quotation
are used when you do the same in python (or else
python thinks it as a variable)
If there is just on element dont use curly brackets
{ '$project': { "_id": 1, "Address.City":1 } } ,
Date Format
ISODate("2002-01-12T20:15:31Z")
'Address.City': 1} } ,
} , { $sort : { 'count' : -1 }} ] )
The brackets {} comes in only when you give more than
one entry
_id should not be in brackets
The aggregate bracket starts with ( but when you have
group, sort and project you use []
db.getCollection('PropertyDetail')._id,
db.getCollection('PropertyDetail').AreaTotalsTotal
);
};
var reduceFunction1 = function(keyCustId, valuesPrices) {
return Array.sum(valuesPrices);
};
db.orders.mapReduce(
mapFunction1,
reduceFunction1,
{ out: "map_reduce_example" }
)
Average
db.getCollection('PropertyDetail').aggregate([{$group:
{_id:null, pop: {$avg:"$BuildingCount"} } }])
'BuildingCount'
);
,
function(key_Id, valuesPrices) {
return Array.avg(valuesPrices);
}
}
,{
out : "resultName"
)
String search
db.getCollection('Jobs').find({"Description" :
/building/}).count() =11k
db.getCollection('Jobs').find({"Description" :
{$regex : /.*violation.*/}}).count()
db.getCollection('Jobs').find({"Description" : {$regex :
".*violation.*"}}).count()
Regax is not case sensitive
Groupby Max
db.getCollection('RiskScores').aggregate(
[ { $project: {
'EntityId': 1} } ,
{
$group : { _id : '$Date' , max: {"$max": 1} }
} , { $sort : { 'Date' : -1 }} ] )
db.getCollection('PropertyDetail').find({'_id':
ObjectId("54bd12a8cbda71674c024b9f")})
db.getCollection('Violations').aggregate(
[ { $project: {
{
'Status': 1} } ,
} , { $sort : { 'count' : -1 }} ] )
Type in MongoDB
Refering agency and so on 1279029.000000
db.getCollection('Violations').aggregate(
[ { $project: {
{
'Type': 1} } ,
} , { $sort : { 'count' : -1 }} ] )
Using PipeLine
db.getCollection('Violations').aggregate(
[
$match: { 'IssuanceAuthorities.0.DateIssued':
{'$gte':ISODate("2009-02-08 00:00:00.000Z") } }
}
{ $project: {
{
'Type': 1} } ,
} , { $sort : { 'count' : -1 }} ] )
'Type': 1} } ,
} , { $sort : { 'count' : -1 }} ] )
db.getCollection('Violations').find({'IssuanceAuthorities.
0.DateIssued': {'$gte':("2011-03-26 00:00:00.000Z")}})
Playing Joins
db.getCollection('Violations').aggregate([
{$match: {
'IssuanceAuthorities.0.DateIssued': {$gte:ISODate("2009-02-08 00:00:00.000Z")}}
},
{$lookup: {
from: 'Properties',
localField: 'PropertyId',
foreignField: '_id',
as: "NameSJ"}
}
])
db.getCollection('Violations').aggregate([
{$match: {
'IssuanceAuthorities.0.DateIssued': {$gte:ISODate("2015-08-08 00:00:00.000Z")}}},
{$lookup: {
from: 'Properties',
localField: 'PropertyId',
foreignField: '_id',
as: "NameSJ"}
} ,
{$project: {
}} ])
_id: 0,
name: "$BuildingName"
db.getCollection('Violations').aggregate([
{$match: {
'IssuanceAuthorities.0.DateIssued': {$gte:ISODate("2015-08-08 00:00:00.000Z")}}},
{$lookup: {
from: 'Properties',
localField: 'PropertyId',
foreignField: '_id',
as: "NameSJ"}
} , {$project: {
_id: 0,
}} ])
name: "$BuildingName"
db.getCollection('Violations').aggregate([
{$match: {
'IssuanceAuthorities.0.DateIssued':
{$gte:ISODate("2015-08-08 00:00:00.000Z")}}},
{ '$group' : { "_id" : '$PropertyId' , 'countPid': {"$sum":
1} }} ,
{$lookup: {
from: 'Properties',
localField: '_id',
as: "NameSJ"}
} , {$project: {
_id:1 ,
namept:
"$NameSJ.PropertyType.Main" ,
name: "$NameSJ.Name"
{
$sort: {namept: 1}
}} ,
}, ])
foreignField: '_id',
db.getCollection('Violations').aggregate([
{$match: {
'IssuanceAuthorities.0.DateIssued': {$gte:ISODate("2015-09-09
00:00:00.000Z")}}},
{ $group :
{ '_id' : "$PropertyId"
,
'Lastdate': { $max: "$IssuanceAuthorities.DateIssued" }
}
}
])
db.getCollection('Violations').aggregate([
{$match: {
'IssuanceAuthorities.0.DateIssued': {$gte:ISODate("2015-08-08
00:00:00.000Z")}}},
{ $project : {
'PropertyId' : 1 ,
'_id' : 1,
'IssuanceAuthorities.0.DateIssued' : 1
}},
{ '$group' : { "_id" :{ "date":
'$IssuanceAuthorities.0.DateIssued' ,"propid":'PropertyId' }
db.getCollection('Violations').find({'Status':'Pending','Da
teClosed' :{$exists:false}}).count()
db.getCollection('Violations').find({'DateClosed'
:null}).count()
localField: '_id',
foreignField: '_id',
as: "NameSJ"}
} , {$project: {
_id:1 ,
namept: "$NameSJ.PropertyType.Main" ,
name: "$NameSJ.Name"
{
$sort: {namept: 1}
}} ,
}, ])
from: 'Properties',
localField: '_id',
foreignField:
'_id',
as: "NameSJ"}
} , {$project: {
_id:1 }} ,
{
$sort: {_id: 1} }, ])
foreignField:
{$sort: {namept: 1} },
{$match: { 'namept': 'Chicago'}}
])
localField: '_id',
db.getCollection('Jobs').aggregate([
from: 'Properties',
localField: '_id',
foreignField: '_id',
as: "NameSJ"}
} , {$project: {
_id:1 ,
namept: "$NameSJ.PropertyType.Main" ,
name: "$NameSJ.Name"
}} ,
{
$sort: {namept: 1} },
])
{$match: {
'NameSJ.PropertyType.Main': 'Retail'}}
Groupby Play
db.getCollection('Violations').aggregate([
{ '$group' : { "_id" : '$PropertyId' , 'countPid':
{"$sum": 1} }} ,
{$project: {_id:1 , 'countPid':1, }} ,
])
}}
, { '$group' : { "_id" : '$PropertyId'
, 'countPid': {"$avg":"$year"}
Links:
http://usuaris.tinet.cat/bertolin/pdfs/mongodb_%20the
%20definitive%20guide%20-%20kristina%20chodorow_1401.pdf
https://docs.mongodb.org/v3.0/reference/method/js-collection/
https://docs.mongodb.org/manual/tutorial/model-tree-structureswith-nested-sets/
https://docs.mongodb.org/v3.0/reference/method/db.collection.a
ggregate/
http://www.tutorialspoint.com/mongodb/mongodb_map_reduce.
htm
https://docs.mongodb.org/manual/tutorial/query-documents/