您的位置:首页 > 数据库 > Mongodb

mongodb聚合

2016-07-16 20:44 253 查看
1.

   aggregation

1.1 $project

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )

{ "_id" : 1, "title" : "abc123", "author" : { "last" : "zzz", "first" : "aaa" } }


或者

{ _id: 1, user: "1234", stop: { title: "book1", author: "xyz", page: 32 } }
{ _id: 2, user: "7890", stop: [ { title: "book2", author: "abc", page: 5 }, { title: "book3", author: "ijk", page: 100 } ] }

db.bookmarks.aggregate( [ { $project: { stop: { title: 1 } } } ] )

{ "_id" : 1, "stop" : { "title" : "book1" } }
{ "_id" : 2, "stop" : [ { "title" : "book2" }, { "title" : "book3" } ] }

或者

{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}

db.books.aggregate(
[
{
$project: {
title: 1,
isbn: {
prefix: { $substr: [ "$isbn", 0, 3 ] },
group: { $substr: [ "$isbn", 3, 2 ] },
publisher: { $substr: [ "$isbn", 5, 4 ] },
title: { $substr: [ "$isbn", 9, 3 ] },
checkDigit: { $substr: [ "$isbn", 12, 1] }
},
lastName: "$author.last",
copiesSold: "$copies"
}
}
]
)

{
"_id" : 1,
"title" : "abc123",
"isbn" : {
"prefix" : "000",
"group" : "11",
"publisher" : "2222",
"title" : "333",
"checkDigit" : "4"
},
"lastName" : "zzz",
"copiesSold" : 5
}


$group

"_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }

db.sales.aggregate(
[
{
$group : {
_id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
]
)

{ "_id" : { "month" : 3, "day" : 15, "year" : 2014 }, "totalPrice" : 50, "averageQuantity" : 10, "count" : 1 }
{ "_id" : { "month" : 4, "day" : 4, "year" : 2014 }, "totalPrice" : 200, "averageQuantity" : 15, "count" : 2 }
{ "_id" : { "month" : 3, "day" : 1, "year" : 2014 }, "totalPrice" : 40, "averageQuantity" : 1.5, "count" : 2 }


2. group:

    group做的聚合有些复杂。先选定分组所依据的键,此后MongoDB就会将集合依据选定键值的不同分成若干组。然后可以通过聚合每一组内的文档,产生一个结果文档。
    --这里是准备的测试数据
    > db.test.remove()
    > db.test.insert({"day" : "2012-08-20", "time" : "2012-08-20 03:20:40", "price" : 4.23})
    > db.test.insert({"day" : "2012-08-21", "time" : "2012-08-21 11:28:00", "price" : 4.27})
    > db.test.insert({"day" : "2012-08-20", "time" : "2012-08-20 05:00:00", "price" : 4.10})
    > db.test.insert({"day" : "2012-08-22", "time" : "2012-08-22 05:26:00", "price" : 4.30})
    > db.test.insert({"day" : "2012-08-21", "time" : "2012-08-21 08:34:00", "price" : 4.01})
    --这里将用day作为group的分组键,然后取出time键值为最新时间戳的文档,同时也取出该文档的price键值。
    > db.test.group( {
    ... "key" : {"day":true},           --如果是多个字段,可以为{"f1":true,"f2":true}
    ... "initial" : {"time" : "0"},       --initial表示$reduce函数参数prev的初始值。每个组都有一份该初始值。
    ... "$reduce" : function(doc,prev) {  --reduce函数接受两个参数,doc表示正在迭代的当前文档,prev表示累加器文档。
    ...     if (doc.time > prev.time) {
    ...         prev.day = doc.day
    ...         prev.price = doc.price;
    ...         prev.time = doc.time;
    ...     }
    ... } } )
    [
        {
            "day" : "2012-08-20",
            "time" : "2012-08-20 05:00:00",
            "price" : 4.1
        },
        {
            "day" : "2012-08-21",
            "time" : "2012-08-21 11:28:00",
            "price" : 4.27
        },
        {
            "day" : "2012-08-22",
            "time" : "2012-08-22 05:26:00",
            "price" : 4.3
        }
    ]

--下面的例子是统计每个分组内文档的数量。
    > db.test.group( {
    ... key: { day: true},
    ... initial: {count: 0},
    ... reduce: function(obj,prev){ prev.count++;},
    ... } )
    [
        {
            "day" : "2012-08-20",
            "count" : 2
        },
        {
            "day" : "2012-08-21",
            "count" : 2
        },
        {
            "day" : "2012-08-22",
            "count" : 1
        }
    ]
    --最后一个是通过完成器修改reduce结果的例子。
    > db.test.group( {
    ... key: { day: true},
    ... initial: {count: 0},
    ... reduce: function(obj,prev){ prev.count++;},
    ... finalize: function(out){ out.scaledCount = out.count * 10 } --在结果文档中新增一个键。
    ... } )
    [
        {
            "day" : "2012-08-20",
            "count" : 2,
            "scaledCount" : 20
        },
        {
            "day" : "2012-08-21",
            "count" : 2,
            "scaledCount" : 20
        },
        {
            "day" : "2012-08-22",
            "count" : 1,
            "scaledCount" : 10
        }    
    ]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: