在数组 mongodb 中使用 concat 时出现问题
Problemm using concat in array mongodb
我有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"
感谢您所做的一切,我对您的代码做了一些修改
$lookup
加入 collections。我从 Flim collection 开始
$unwind
解构数组
$group
重建我们已经解构的数组,这将
- 由于我们有嵌套数组,我们需要使用
$map
遍历它们以收集名字和姓氏
- 以上阶段将再次以嵌套数组结束,因此我们使用
$reduce
再次循环并使用 $setUnion
删除内部数组
删除一些重复条目,具体取决于您的要求
这是代码
db.Film.aggregate([
{
$lookup: {
from: "Film_Actor",
localField: "film_id",
foreignField: "film_id",
as: "join_flim"
}
},
{ "$unwind": "$join_flim" },
{
$lookup: {
from: "Actor",
localField: "join_flim.actor_id",
foreignField: "actor_id",
as: "join_flim.join_actor"
}
},
{
$group: {
_id: "$_id",
title: { $first: "$title" },
join_flim: { $push: "$join_flim" }
}
},
{
"$project": {
title: 1,
actornames: {
$map: {
input: "$join_flim",
as: "f",
in: {
$map: {
input: "$$f.join_actor",
as: "a",
in: {
$concat: [ "$$a.first_name", " ", "$$a.last_name" ]
}
}
}
}
}
}
},
{
"$project": {
title: 1,
actornames: {
"$reduce": {
"input": "$actornames",
"initialValue": [],
"in": {
"$setUnion": [ "$$this", "$$value" ]
}
}
}
}
}
])
我有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"
感谢您所做的一切,我对您的代码做了一些修改
$lookup
加入 collections。我从 Flim collection 开始
$unwind
解构数组$group
重建我们已经解构的数组,这将- 由于我们有嵌套数组,我们需要使用
$map
遍历它们以收集名字和姓氏 - 以上阶段将再次以嵌套数组结束,因此我们使用
$reduce
再次循环并使用$setUnion
删除内部数组
删除一些重复条目,具体取决于您的要求
这是代码
db.Film.aggregate([
{
$lookup: {
from: "Film_Actor",
localField: "film_id",
foreignField: "film_id",
as: "join_flim"
}
},
{ "$unwind": "$join_flim" },
{
$lookup: {
from: "Actor",
localField: "join_flim.actor_id",
foreignField: "actor_id",
as: "join_flim.join_actor"
}
},
{
$group: {
_id: "$_id",
title: { $first: "$title" },
join_flim: { $push: "$join_flim" }
}
},
{
"$project": {
title: 1,
actornames: {
$map: {
input: "$join_flim",
as: "f",
in: {
$map: {
input: "$$f.join_actor",
as: "a",
in: {
$concat: [ "$$a.first_name", " ", "$$a.last_name" ]
}
}
}
}
}
}
},
{
"$project": {
title: 1,
actornames: {
"$reduce": {
"input": "$actornames",
"initialValue": [],
"in": {
"$setUnion": [ "$$this", "$$value" ]
}
}
}
}
}
])