Mongodb aggregate arguments to $lookup must be strings

后端 未结 2 1253
暗喜
暗喜 2021-01-17 16:04
        db.absences.insert([
           { \"_id\" : 1, \"student\" : \"Ann Aardvark\", sickdays: [ new Date (\"2018-05-01\"),new Date (\"2018-08-23\") ] },
                  


        
2条回答
  •  Happy的楠姐
    2021-01-17 16:32

    In $lookup you could just join the collectionfirst and doing the match, project and etc later after lookup. And you should have the foreign field that referring to holidays collection.

    Like :

    {
      $lookup:
         {
           from: "holidays",
           localField: "holidaysID", //yourLocalfield, it is local field in absences. you should have it to join 2 collections
           foreignField : "_id", //theforeignfield, it is _id in the holidays
           as: "holidays"
         }
    },{ $match: { year: 2018 } },
    { $project: { _id: 0, date: { name: "$name", date: "$date" } } },
    { $replaceRoot: { newRoot: "$date" } }
    

    Hope it helps.

提交回复
热议问题