mongo学习- mapReduce操作事例

源数据:

{
    "_id" : 1.0,
    "name" : "abc",
    "age" : 43.0,
    "type" : 1.0,
    "status" : "P",
    "favorites" : {
        "artist" : "Picasso",
        "food" : "pizza"
    },
    "finished" : [ 
        17.0, 
        3.0
    ],
    "badges" : [ 
        "blue", 
        "black"
    ],
    "points" : [ 
        {
            "points" : 85.0,
            "bonus" : 20.0
        }, 
        {
            "points" : 85.0,
            "bonus" : 10.0
        }
    ]
}

/* 2 */
{
    "_id" : 2.0,
    "name" : "abc",
    "age" : 42.0,
    "type" : 1.0,
    "status" : "A",
    "favorites" : {
        "artist" : "Miro",
        "food" : "meringue"
    },
    "finished" : [ 
        11.0, 
        25.0
    ],
    "badges" : [ 
        "green"
    ],
    "points" : [ 
        {
            "points" : 85.0,
            "bonus" : 20.0
        }, 
        {
            "points" : 64.0,
            "bonus" : 12.0
        }
    ]
}

/* 3 */
{
    "_id" : 3.0,
    "name" : "ahn",
    "age" : 43.0,
    "type" : 2.0,
    "status" : "A",
    "favorites" : {
        "artist" : "Cassatt",
        "food" : "cake"
    },
    "finished" : [ 
        6.0
    ],
    "badges" : [ 
        "blue", 
        "red"
    ],
    "points" : [ 
        {
            "points" : 81.0,
            "bonus" : 8.0
        }, 
        {
            "points" : 55.0,
            "bonus" : 20.0
        }
    ]
}

/* 4 */
{
    "_id" : 4.0,
    "name" : "xi",
    "age" : 34.0,
    "type" : 2.0,
    "status" : "D",
    "favorites" : {
        "artist" : "Chagall",
        "food" : "chocolate"
    },
    "finished" : [ 
        5.0, 
        11.0
    ],
    "badges" : [ 
        "red", 
        "black"
    ],
    "points" : [ 
        {
            "points" : 53.0,
            "bonus" : 15.0
        }, 
        {
            "points" : 51.0,
            "bonus" : 15.0
        }
    ]
}

/* 5 */
{
    "_id" : 5.0,
    "name" : "xyz",
    "age" : 23.0,
    "type" : 2.0,
    "status" : "D",
    "favorites" : {
        "artist" : "Noguchi",
        "food" : "nougat"
    },
    "finished" : [ 
        14.0, 
        6.0
    ],
    "badges" : [ 
        "orange"
    ],
    "points" : [ 
        {
            "points" : 71.0,
            "bonus" : 20.0
        }
    ]
}

/* 6 */
{
    "_id" : 6.0,
    "name" : "abc",
    "age" : 43.0,
    "type" : 1.0,
    "status" : "A",
    "favorites" : {
        "food" : "pizza",
        "artist" : "Picasso"
    },
    "finished" : [ 
        18.0, 
        12.0
    ],
    "badges" : [ 
        "black", 
        "blue"
    ],
    "points" : [ 
        {
            "points" : 78.0,
            "bonus" : 8.0
        }, 
        {
            "points" : 57.0,
            "bonus" : 7.0
        }
    ]
}

  

var mrr=db.users.mapReduce(
function(){
emit(this.age,this.name);
},
function(key,values){
var a=Avalues.length;

var ret={age:key,names:a};
return ret;
},
{ out: {replace:"result"}}

);
db[mrr.result].find()

 

posted @ 2018-08-14 16:53  anxbb  阅读(119)  评论(0编辑  收藏  举报