單一用途的聚合方法:db.collection.count(), db.collection.group(), db.collection.distinct()。
一:db.collection.count() 返回匹配查詢結果的數量
db.collection.count(query, options) 返回匹配查詢結果的數量 相當於mysql的語法:select count(1) from orders where 條件。
> db.orders.find()
{ "_id" : ObjectId("57383f492bd2092c7ed0fec7"), "ino" : "001", "quantity" : 2, "
price" : 4 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec8"), "ino" : "002", "quantity" : 2, "
price" : 6 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec9"), "ino" : "003", "quantity" : 3, "
price" : 5 }
> db.orders.count()
3
> db.orders.count({quantity:{$gt:2}})
1
也可以這樣獲取返回匹配查詢結果的數量:
var cursor=db.items.find() 可以以查詢只包含索引鍵的條件, cursor.count()。
二: db.collection.distinct() 返回某個字段的非重複值列表
> db.orders.find()
{ "_id" : ObjectId("57383f492bd2092c7ed0fec7"), "ino" : "001", "quantity" : 2, "
price" : 4 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec8"), "ino" : "002", "quantity" : 2, "
price" : 6 }
{ "_id" : ObjectId("57383f492bd2092c7ed0fec9"), "ino" : "003", "quantity" : 3, "
price" : 5 }
> db.orders.distinct("quantity")
[ 2, 3 ]
三: db.collection.group()
(1)在2.2版本中,返回的數組可以包含最多20000個元素;即最多20000個獨特的分組。
(2)聚集管道(Aggregation),Map-Reduce方法都可以運行在分片集合,group()方法不能運行在分片集羣中工作。
1. 訂單集合記錄日期和明細中的數量、產品編碼,我們訂單集合按照對日期和產品編碼進行分組字段,然後對每一組文檔進行處理,找出並計算相同的產品的數量。Sql語句:Select pnumber,sum(quantity) as total from orders,items group by pnumber(少了兩張表的關聯的條件)。
> db.orders.find()
{ "_id" : ObjectId("573848342bd2092c7ed0feca"), "onumber" : "001", "date" : ISOD
ate("2014-01-02T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 2, "price"
: 5, "pnumber" : "p003" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecb"), "onumber" : "002", "date" : ISOD
ate("2014-01-03T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 1, "price"
: 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecc"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 10, "price
" : 2, "pnumber" : "p001" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecd"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 30, "price
" : 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fece"), "onumber" : "004", "date" : ISOD
ate("2014-01-05T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 5, "price"
: 4, "pnumber" : "p002" } }
> db.orders.group({
... key: {'item.pnumber':1},
... initial : {"total":0},
... reduce : function Reduce(doc, out) {
... out.total+=doc.item.quantity
... } });
[
{
"item.pnumber" : "p003",
"total" : 2
},
{
"item.pnumber" : "p002",
"total" : 36
},
{
"item.pnumber" : "p001",
"total" : 10
}
]
key:作爲分組的key。
initial:初始化聚合結果文檔變量,爲空時自動爲每列提供初始變量。
keyf:可選。替代的key 字段。指定一個函數創建一個“key object”作爲分組的key。使用keyf而是通過group by領域而不是現有的文檔域鍵組。
cond:過濾條件,根據條件過濾集合的文檔。
(1) 我們對訂單集合根據日期進行分組,並對相同的產品號數量進行累加,對累加完的產品數量沒有大於20 的我們進行刪除。減少返回的數據。
db.orders.group({
key: {date:1},
initial : {"pnumbers":{}},
reduce : function Reduce(doc, out) {
if(out.pnumbers[doc.item.pnumber]==null){
out.pnumbers[doc.item.pnumber]=new Object();
out.pnumbers[doc.item.pnumber]=doc.item.quantity;
}else{
out.pnumbers[doc.item.pnumber]+=doc.item.quantity;
}
},finalize : function Finalize(doc) {
for(i in doc.pnumbers)
{
if (doc.pnumbers[i] < 20)
{
delete doc.pnumbers[i];
}
}
} });
[
{
"date" : ISODate("2014-01-02T16:03:00Z"),
"pnumbers" : {
}
},
{
"date" : ISODate("2014-01-03T16:03:00Z"),
"pnumbers" : {
}
},
{
"date" : ISODate("2014-01-04T16:03:00Z"),
"pnumbers" : {
"p002" : 30
}
},
{
"date" : ISODate("2014-01-05T16:03:00Z"),
"pnumbers" : {
}
}
]
db.orders.group({
key: {date:1},
initial :{"total":0,"money":0},
reduce : function Reduce(doc, out) {
out.total+=doc.item.quantity;
out.money+=doc.item.quantity*doc.item.price;
},
finalize : function Finalize(out) {
out.avg=out.money/out.total
return out;
}
});
[
{
"date" : ISODate("2014-01-02T16:03:00Z"),
"total" : 2,
"money" : 10,
"avg" : 5
},
{
"date" : ISODate("2014-01-03T16:03:00Z"),
"total" : 1,
"money" : 4,
"avg" : 4
},
{
"date" : ISODate("2014-01-04T16:03:00Z"),
"total" : 40,
"money" : 140,
"avg" : 3.5
},
{
"date" : ISODate("2014-01-05T16:03:00Z"),
"total" : 5,
"money" : 20,
"avg" : 4
}
]
> db.orders.find({})
{ "_id" : ObjectId("573848342bd2092c7ed0feca"), "onumber" : "001", "date" : ISOD
ate("2014-01-02T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 2, "price"
: 5, "pnumber" : "p003" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecb"), "onumber" : "002", "date" : ISOD
ate("2014-01-03T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 1, "price"
: 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecc"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 10, "price
" : 2, "pnumber" : "p001" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fecd"), "onumber" : "003", "date" : ISOD
ate("2014-01-04T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 30, "price
" : 4, "pnumber" : "p002" } }
{ "_id" : ObjectId("573848342bd2092c7ed0fece"), "onumber" : "004", "date" : ISOD
ate("2014-01-05T16:03:00Z"), "cname" : "zcy", "item" : { "quantity" : 5, "price"
: 4, "pnumber" : "p002" } }
> db.orders.group({
... keyf: function (doc){
... return{'month':doc.date.getMonth()+1};
... },
... initial :{"total":0,"money":0},
... reduce : function Reduce(doc, out) {
... out.total+=doc.item.quantity;
... out.money+=doc.item.quantity*doc.item.price;
...
... },
... finalize : function Finalize(out) {
... out.avg=out.money/out.total
... return out;
... }
... });
[ { "month" : 1, "total" : 48, "money" : 174, "avg" : 3.625 } ]