【发布时间】:2021-08-17 11:23:44
【问题描述】:
我有 3 张桌子
1) Actor: actor_id, first_name, last_name
2) Film: film_id, title
3) Film_Actor: film_id, actor_id
示例文档:
_id
:
60aedac769985522a024daca
actor_id
:
"1"
first_name
:
"Penelope"
last_name
:
"Guiness"
我想要的结果不仅是 first_name,还有 last_name。我在$group 函数中使用concat 时遇到问题。
我的完整代码:
db.film.aggregate([
{
$lookup: {
from: "film_actor",
localField: "film_id",
foreignField: "film_id",
as: "film_actor"
}
},
{
$lookup: {
from: "actor",
localField: "film_actor.actor_id",
foreignField: "actor_id",
as: "actor"
}
},
{
$group: {
_id: "$film_id",
title: {"$first":"$title"},
name: {$push: "$actor.first_name"}
}
}
]);
错误报告:
$concat only supports strings, not array
想要的输出:
id:"207"
title:"Dangerous Uptown"
name:Array
0:"Penelope Guiness"
1:"Mary Watson"
2:"Ralph Holts"
3:"Spencer Dani"
【问题讨论】:
-
发布您收藏的示例文档。
-
你能告诉我们你的样本数据是什么样子吗?
-
@turivishal 完成,我已编辑问题
-
@varman 完成,我已编辑问题
-
请发布有效的 json 格式文档,我在您的文档中看不到
film_id字段并且您已在 $group 中使用。
标签: arrays mongodb aggregation