MongoDb部门不在查询中工作

时间:2017-05-28 16:11:26

标签: mongodb

这就是我的数据的样子

{
  "_id" : ObjectId("5919f19716e64ad662bd753b"),
  "cardNumber" : "11111111115523",
  "transID" : "5645344133546",
  "transDate" : "1/1/16",
  "merchant" : "Publix",
  "mtype" : "Groceries",
  "merchantCode" : "1",
  "transAmount" : 100
}

/* 2 */
{
  "_id" : ObjectId("5919f20416e64ad662bd753c"),
  "cardNumber" : "11111111115523",
  "transID" : "5645344136544",
  "transDate" : "1/4/16",
  "merchant" : "Sears",
  "mtype" : "Clothing and Shoes",
  "merchantCode" : "2",
  "transAmount" : 150.66
}
 /* 3 */
{
  "_id" : ObjectId("5919f20c16e64ad662bd753d"),
  "cardNumber" : "11111111115523",
  "transID" : "564535522233344",
  "transDate" : "1/4/16",
  "merchant" : "Shell",
  "mtype" : "Gasoline / Fuel",
  "merchantCode" : "3",
  "transAmount" : 25
}

我正在寻找的结果是:  所有" transAmount"的总和乘以100并除以该人花费的总数(例如:(totalByCategories)* 100 /(sumOfAllTransactiosAmount))然后输出需要按" merchantCode"这样每个子集都有操作的类别和相应的结果 我让他们单独工作,但当我尝试进行完整查询时,它会给出错误代码16420聚合失败。

以下是我的查询的样子:

var denominator = db.getCollection('Transactions').aggregate({
$group: { 
    "_id": null, 
    "total": {$sum:"$transAmount"}
  }
}, { $project: {_id:0, total: "$total"}});

db.getCollection('Transactions').aggregate([{
$group: { 
    _id: '$merchantCode', 
    total: {
        $sum: { $multiply: ['$transAmount', 100 ]}

 }}},
   { $project: { 

       percentage: { 
         $divide: [ 
           "$total", 
          denominator
          ] 
      } 
   }
 }
])

提前感谢您的帮助

0 个答案:

没有答案