15

如何为 Mongo 转换以下 SQL 查询:

SELECT KeyID, SUM(Qty)
FROM Table
WHERE Date <= '1998-12-01' 
4

2 回答 2

52

就像是:

db.myTable.aggregate([
  { $match: { mydate: { $lte: new Date('12/01/1998') } } },
  { $group: {
    _id: "$keyid",
    totalqty: { $sum: "$qty" }
  }}
])
于 2012-11-08T18:04:24.737 回答
0

可以用这个——

db.schoolexamregistration.aggregate([
    { $match: {"exam_session_id":"1523850138707"} },{
     $lookup:
       {
         from: "student",
         localField: "nic_ppt",
         foreignField: "nic_ppt",
         as: "student_detail"
       }
    }
  ])
于 2018-04-16T05:47:50.113 回答