Mongodb aggregate arguments to $lookup must be strings

后端 未结 2 1182
暗喜
暗喜 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条回答
  • 2021-01-17 16:07

    Because you are trying to use the $lookup features (syntax) from MongoDB v3.6 on MongoDB v3.4

    The MongoDB v3.4 $lookup syntax:

    {
       $lookup:
         {
           from: <collection to join>,
           localField: <field from the input documents>,
           foreignField: <field from the documents of the "from" collection>,
           as: <output array field>
         }
    }
    

    The MongoDB v3.6 $lookup syntax:

    {
       $lookup:
         {
           from: <collection to join>,
           let: { <var_1>: <expression>, …, <var_n>: <expression> },
           pipeline: [ <pipeline to execute on the collection to join> ],
           as: <output array field>
         }
    }
    

    https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题