ryanwold.net

A civic-minded citizen seeking the singularity

An entry

Code Notes from when learning Map Reduce with MongoDB

Date: 2013-09-24
Status: release
Tags: notes mongodb

``` db.earthquakes.aggregate( [ { $project : { monthjoined : { $month : "$joined" } } } , { $group : { _id : {monthjoined:"$monthjoined"} , number : { $sum : 1 } } }, { $sort : { "id.month_joined" : 1 } } ] )

{ $project : { month_joined : "$time" } },

db.earthquakes.aggregate( [ { $project : { dangerscore : "$time" } }, { $group : { _id : { dangerscore: "$dangerscore" } , number : { $sum : 1 } } }, { $sort : { "id.month_joined" : 1 } } ] )

score = $geoNear: { near: [40.724, -73.997], distanceField: "dist.calculated", maxDistance: 0.008, query: { type: "public" }, includeLocs: "dist.location", uniqueDocs: true, num: 5 }

// example db.collection.aggregate ( [ {$group : {id : "$product", count : {$sum:"$cCount"}, cpids : {$push : "$cpid"} } }, {$unwind : "$cpids"}, {$group : {id : "$_id", cCount : {$first:"$count"}, cpid : {$addToSet : "$cpids"} } } ] )

db.earthquakes.aggregate([ { $geoNear: { near: [-64.0, 19.0], distanceField: 'dist_calculated', spherical: true, maxDistance: 25 / 3569 } } ])

// Step 1

db.earthquakes.aggregate( [ { $geoNear: { near: [-122, 37], distanceField: "dist_calculated", spherical: true, maxDistance: 50.0 / 3959.0, } } ] )

db.earthquakes.find().limit(10).forEach(function(quake){ quakes = db.runCommand({ geoNear: "earthquakes", maxDistance: 25 / 3969, near: quake.coordinates, spherical: true, limit: 5000 });

z = db.earthquakes.aggregate( [ { $geoNear: { near: quake.coordinates, distanceField: "dist.calculated", spherical: true, maxDistance: 50.0 / 3959.0, } }, { $group: { _id: "", total: { $sum: "$mag" }, count: { $sum: 1 } } } ] );

printjson(quake.uid, z) });

db.earthquakes.aggregate( [ { $geoNear: { near: quake.coordinates, distanceField: "dist.calculated", spherical: true, maxDistance: 50.0 / 3959.0, } }, { $group: { _id: "", total: { $sum: "$mag" }, count: { $sum: 1 } } } ] );

db.earthquakes.aggregate( [ { $geoNear: { near: [-122, 37], distanceField: "dist.calculated", spherical: true, maxDistance: 50.0 / 3959.0, } }, { $group: { _id: "", total: { $sum: "$mag" }, count: { $sum: 1 } } } ] )

{ $project : { danger_score: "$result.length()" } }

{ $group : { id : { dangerscore: "$danger_score" } , number : { $sum : 1 } } }

db.earthquakes.aggregate( [ { $project: { dangerscore: { $geoNear: { near: [40.724, -73.997], distanceField: "dist.calculated", maxDistance: 0.008, query: { type: "public" }, includeLocs: "dist.location", uniqueDocs: true, num: 5 } } } }, { $group : { _id : { dangerscore: "$dangerscore" } , number : { $sum : 1 } } }, { $sort : { "id.danger_score" : 1 } } ] )

$geoNear: { near: [40.724, -73.997], distanceField: "dist.calculated", maxDistance: 0.008, query: { type: "public" }, includeLocs: "dist.location", uniqueDocs: true, num: 5 } ```