Mongodb聚合操作中$lookup的参数必须为字符串。

10
        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。

2个回答

32

由于您正在尝试在MongoDB v3.4上使用$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/


1
我参考了这份文档并尝试了它,这也是我在问题中提到的。请在您的机器上尝试一下。它会给你相同的错误。 - Nakarmi
本教程将帮助您升级MongoDB到3.6: https://optimalbi.com/blog/2018/05/16/upgrading-mongodb-3-4-to-3-6-on-ubuntu-16-04-easy-as-microwave-pie/ - Aboozar Rajabi
我使用MongoDB v4.4,但仍然遇到此错误,请您指导我。 - Raha Shafaei

-2
在 $lookup 中,您可以先加入集合并进行匹配、投影等操作,然后再进行后续的操作。同时,您需要确保外键字段引用了 holidays 集合。
例如:
{
  $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" } }

希望能有所帮助。


这取决于MongoDB的版本。请查看文档 https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries (-; - Neodan
谢谢您的回答,但这并不能解决我的问题。$lookup从holidays中获取所有文档,而我需要从一个文档中仅获取所选数据。 - Nakarmi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接