【问题标题】:Mongodb aggregate arguments to $lookup must be strings$lookup 的 Mongodb 聚合参数必须是字符串
【发布时间】:2018-05-20 01:54:45
【问题描述】:
        db.absences.insert([
           { "_id" : 1, "student" : "Ann Aardvark", sickdays: [ new Date ("2018-05-01"),new Date ("2018-08-23") ] },
           { "_id" : 2, "student" : "Zoe Zebra", sickdays: [ new Date ("2018-02-01"),new Date ("2018-05-23") ] },
        ])

    db.holidays.insert([
       { "_id" : 1, year: 2018, name: "New Years", date: new Date("2018-01-01") },
       { "_id" : 2, year: 2018, name: "Pi Day", date: new Date("2018-03-14") },
       { "_id" : 3, year: 2018, name: "Ice Cream Day", date: new Date("2018-07-15") },
       { "_id" : 4, year: 2017, name: "New Years", date: new Date("2017-01-01") },
       { "_id" : 5, year: 2017, name: "Ice Cream Day", date: new Date("2017-07-16") }
    ])

db.absences.aggregate([
   {
      $lookup:
         {
           from: "holidays",
           pipeline: [
              { $match: { year: 2018 } },
              { $project: { _id: 0, date: { name: "$name", date: "$date" } } },
              { $replaceRoot: { newRoot: "$date" } }
           ],
           as: "holidays"
         }
    }
])

我正在尝试使用管道查找聚合查询。与 Mongodb 文档中的相同,它仍然给出错误

Unable to execute the selected commands

Mongo Server error (MongoCommandException): Command failed with error 4570: 'arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: "$name", date: "$date" } } }, { $replaceRoot: { newRoot: "$date" } } ] is type array' on server localhost:27017. 

The full response is:
{ 
    "ok" : 0.0, 
    "errmsg" : "arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: \"$name\", date: \"$date\" } } }, { $replaceRoot: { newRoot: \"$date\" } } ] is type array", 
    "code" : NumberInt(4570), 
    "codeName" : "Location4570"
}

我正在使用 mongodb v3.4。

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    因为您尝试在 MongoDB v3.4

    上使用 MongoDB v3.6 中的 $lookup 功能(语法)

    MongoDB v3.4 $lookup 语法:

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

    MongoDB v3.6 $lookup 语法:

    {
       $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/

    【讨论】:

    • 我参考了这个文档并尝试了它,我在我的问题中也提到了。请在您的机器上试用。它会给你同样的错误。
    • 本教程帮助将MongoDB升级到3.6:optimalbi.com/blog/2018/05/16/…
    • 我使用的是 MongoDB v4.4 但仍然出现此错误,请指导我。
    【解决方案2】:

    在 $lookup 中,您可以先加入集合,然后在查找后进行匹配、项目等。你应该有提到假期集合的外国领域。

    喜欢:

    {
      $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" } }
    

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 2014-12-11
    • 2020-09-10
    • 2018-04-24
    • 2022-01-05
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2015-03-24
    相关资源
    最近更新 更多