Aggregate query with where condition

三世轮回 提交于 2020-07-05 05:47:08

问题


How could the following SQL query be converted for Mongo:

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

回答1:


Something like:

db.myTable.aggregate([
  { $match: { mydate: { $lte: new Date('12/01/1998') } } },
  { $group: {
    _id: "$keyid",
    totalqty: { $sum: "$qty" }
  }}
])



回答2:


Can use this -

db.schoolexamregistration.aggregate([
    { $match: {"exam_session_id":"1523850138707"} },{
     $lookup:
       {
         from: "student",
         localField: "nic_ppt",
         foreignField: "nic_ppt",
         as: "student_detail"
       }
    }
  ])


来源:https://stackoverflow.com/questions/13294556/aggregate-query-with-where-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!