Thursday, March 13, 2014

Mongo 2.4.9 - MapReduce

1. Map

function map_closest() {
    var pitt = [-80.064879, 40.612044];
    var phil = [-74.978052, 40.089738];

    function distance(a, b) {
        var dx = a[0] - b[0];
        var dy = a[1] - b[1];
        return Math.sqrt(dx * dx + dy * dy);
    }

    if (distance(this.loc, pitt) < distance(this.loc, phil)) {
        emit("pitt", 1);
    } else {
        emit("phil", 1);
    }
}

2. Reduce

2.1. Array.sum()

function reduce_closest(city, counts) {  
  return Array.sum(counts); 
}

2.2. for()

function reduce_closest(city, counts) {
 var total = 0;
 var length = counts.length;

 for (var i = 0; i < length; i++) {
  total += counts[i];
 }

 return total;
}

2.3. forEach()

function reduce_closest(city, counts) {
 var total = 0;

 counts.forEach(function(count) {
  total += count;
 });

 return total;
}

3. mapReduce

db.zips.mapReduce(
  map_closest, 
  reduce_closest, 
  { 
    query: { state: 'PA' }, 
    out: { inline: 1 } 
  }
)

Mongo 2.4.9 - $elemMatch

- db.policies.find( { status : { $ne : "expired" }, coverages : { $elemMatch : { type : "liability", rates : { $elemMatch : { rate : { $gte : 100 }, current : true } } } } } ).pretty()

{
        "_id" : "1024850AB",
        "status" : "draft",
        "insured_item" : {
                "make" : "Cessna",
                "model" : "Skylane",
                "year" : 1982,
                "serial" : "AB1783A"
        },
        "insured_parties" : [
                ObjectId("5097f7351d9a5941f5111d61")
        ],
        "coverages" : [
                {
                        "type" : "liability",
                        "limit" : 1000000,
                        "rates" : [
                                {
                                        "rate" : 200,
                                        "staff_id" : ObjectId("5097f7351d9a5999f5111d69"),
                                        "date" : ISODate("2012-11-05T17:29:54.561Z"),
                                        "current" : true
                                }
                        ]
                },
                {
                        "type" : "property",
                        "deductible" : 5000,
                        "limit" : 100000,
                        "rates" : [
                                {
                                        "rate" : 300,
                                        "staff_id" : ObjectId("5097f7351d9a5999f5111d69"),
                                        "date" : ISODate("2012-11-05T17:29:56.561Z"),
                                        "current" : true
                                }
                        ]
                }
        ],
        "underwriting" : {
                "staff_id" : ObjectId("5097f84cf8dd729bc7273068"),
                "action" : "approved",
                "date" : ISODate("2012-11-05T17:33:00.693Z")
        }
}

Mongo 2.4.9 - Command

- mongoimport -d database -c collection --drop example.json

- mongo example.js
- mongo --shell example.js
- mongo --shell database example.js

Wednesday, March 12, 2014

Mongo 2.4.9 - $substr

- db.zips.findOne()

{
        "city" : "ACMAR",
        "loc" : [
                -86.51557,
                33.584132
        ],
        "pop" : 6055,
        "state" : "AL",
        "_id" : "35004"
}

- db.zips.count()

29467

- db.zips.aggregate({$project:{_id:{$substr:['$city',0,1]}}},{$group:{_id:'$_id',n:{$sum:1}}},{$sort:{n:-1}})

{
        "result" : [
                {
                        "_id" : "S",
                        "n" : 2871
                },
                {
                        "_id" : "C",
                        "n" : 2692
                },
                {
                        "_id" : "M",
                        "n" : 2348
                },
                {
                        "_id" : "B",
                        "n" : 2344
                },
                {
                        "_id" : "W",
                        "n" : 1834
                },
                {
                        "_id" : "L",
                        "n" : 1738
                },
                {
                        "_id" : "P",
                        "n" : 1681
                },
                {
                        "_id" : "H",
                        "n" : 1621
                },
                {
                        "_id" : "A",
                        "n" : 1398
                },
                {
                        "_id" : "G",
                        "n" : 1304
                },
                {
                        "_id" : "R",
                        "n" : 1284
                },
                {
                        "_id" : "D",
                        "n" : 1162
                },
                {
                        "_id" : "N",
                        "n" : 1128
                },
                {
                        "_id" : "F",
                        "n" : 1091
                },
                {
                        "_id" : "E",
                        "n" : 1050
                },
                {
                        "_id" : "T",
                        "n" : 955
                },
                {
                        "_id" : "O",
                        "n" : 767
                },
                {
                        "_id" : "K",
                        "n" : 630
                },
                {
                        "_id" : "J",
                        "n" : 391
                },
                {
                        "_id" : "V",
                        "n" : 381
                },
                {
                        "_id" : "I",
                        "n" : 288
                },
                {
                        "_id" : "U",
                        "n" : 165
                },
                {
                        "_id" : "Y",
                        "n" : 112
                },
                {
                        "_id" : "Q",
                        "n" : 68
                },
                {
                        "_id" : "Z",
                        "n" : 48
                },
                {
                        "_id" : "3",
                        "n" : 22
                },
                {
                        "_id" : "6",
                        "n" : 20
                },
                {
                        "_id" : "4",
                        "n" : 19
                },
                {
                        "_id" : "5",
                        "n" : 15
                },
                {
                        "_id" : "2",
                        "n" : 13
                },
                {
                        "_id" : "7",
                        "n" : 10
                },
                {
                        "_id" : "9",
                        "n" : 8
                },
                {
                        "_id" : "8",
                        "n" : 3
                },
                {
                        "_id" : "0",
                        "n" : 3
                },
                {
                        "_id" : "X",
                        "n" : 2
                },
                {
                        "_id" : "1",
                        "n" : 1
                }
        ],
        "ok" : 1
}

- db.zips.aggregate({$project:{_id:{$substr:['$city',0,1]}}},{$group:{_id:'$_id',n:{$sum:1}}},{$match:{_id:{$in:['0','1','2','3','4','5','6','7','8','9']}}}, {$group:{_id:null,count:{$sum:'$n'}}})

{ "result" : [ { "_id" : null, "count" : 114 } ], "ok" : 1 }

- db.zips.remove({city:/^[0-9]/})

- db.zips.count()

29353

Mongo 2.4.9 - aggregate

-  db.zips.findOne()

{
        "city" : "ACMAR",
        "loc" : [
                -86.51557,
                33.584132
        ],
        "pop" : 6055,
        "state" : "AL",
        "_id" : "35004"
}

- db.zips.aggregate({$group:{_id:'$state',count:{$sum:1}}},{$sort:{count:-1}},{$limit:4})

{
        "result" : [
                {
                        "_id" : "TX",
                        "count" : 1671
                },
                {
                        "_id" : "NY",
                        "count" : 1595
                },
                {
                        "_id" : "CA",
                        "count" : 1516
                },
                {
                        "_id" : "PA",
                        "count" : 1458
                }
        ],
        "ok" : 1
}

Mongo 2.4.9 - $inc

- db.products.find({for:'ac3'}).limit(1).pretty()

{
        "_id" : ObjectId("507d95d5719dbef170f15bf9"),
        "for" : [
                "ac3",
                "ac7",
                "ac9"
        ],
        "name" : "AC3 Series Charger",
        "price" : 21,
        "type" : [
                "accessory",
                "charger"
        ],
        "warranty_years" : 0.25
}

- db.products.update({for:'ac3'},{$inc:{price:2}},0,1)

Mongo 2.4.9 - $exist

- db.products.find({'limits.voice':{$exists:1}}).limit(1).pretty()

{
        "_id" : ObjectId("507d95d5719dbef170f15bfe"),
        "name" : "Phone Service Basic Plan",
        "type" : "service",
        "monthly_price" : 40,
        "limits" : {
                "voice" : {
                        "units" : "minutes",
                        "n" : 400,
                        "over_rate" : 0.05
                },
                "data" : {
                        "units" : "gigabytes",
                        "n" : 20,
                        "over_rate" : 1
                },
                "sms" : {
                        "units" : "texts sent",
                        "n" : 100,
                        "over_rate" : 0.001
                }
        },
        "term_years" : 2
}