Mongodb $ lookup的聚合参数必须是字符串

编程入门 行业动态 更新时间:2024-10-28 00:14:23
本文介绍了Mongodb $ lookup的聚合参数必须是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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文档中的内容相同,它仍然会给出错误消息

I'm trying to use pipeline in lookup for aggregation query. Having this as same as from the Mongodb documentation, it still gives an error

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.

I'm using mongodb v3.4.

推荐答案

因为您正尝试使用 $lookup 功能(语法),来自 MongoDB v3.4

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> } }

docs.mongodb/manual/reference/operator /aggregation/lookup/

更多推荐

Mongodb $ lookup的聚合参数必须是字符串

本文发布于:2023-11-22 17:20:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   参数   Mongodb   lookup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!