MongoDB从聚合查询到组

时间:2016-04-23 00:02:54

标签: mongodb mongodb-query aggregation-framework

我在MongoDB中有以下查询:

var subquery = {"shipdate" : { 
"$gte" : 19960101, 
"$lt" : 19960401 } 
};
var eachsupp = db.lineitems.aggregate([
{ $match : subquery },
{ $project : {
    "_id" : 0,
    "revenue" : {$multiply : ["$extendedprice", {$subtract : [1, "$discount"] }] },
    "supplier_no" : "$partsupp.supplier.suppkey",
    "name" : "$partsupp.supplier.name",
    "address" : "$partsupp.supplier.address",
    "phone" : "$partsupp.supplier.phone" }},
{ $group : {
    _id : "$supplier_no",
    total_revenue : { $sum : "$revenue" },
    name : {$first : "$name"},
    address : {$first : "$address"},
    phone : {$first : "$phone"} }}
]);
eachsupp.forEach(function(document){db.tmp.insert(document)});
db.tmp.find().sort({total_revenue : -1}).limit(1);

但出于学术目的,我需要使用.group()进行此查询。像这样的东西

db.lineitems.group(...);

我是这样开始但我不知道如何进行下一步

var red = function(doc, out){
out.supplier_no
out.revenue_calc = doc.extendedprice*(1-doc.discount);
};

db.lineitems.group({
key : "revenue",
cond : subquery,
initial : {revenue_calc : 0},
reduce : red,
finalize : fin
});

0 个答案:

没有答案