Aggregate query with where condition

2020-07-02 10:17发布

问题:

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"
       }
    }
  ])