如何在Mongo聚集管道中促进/拉动一个级别的孩子领域
我有一个如下所示的集合:
[
{
_id: ObjectId("6255a889fb63822bd51b0356"),
answers: {
"6255a889fb63822bd51b0326": {
value: ObjectId("6255a889fb63822bd51b0329"),
},
"6255a889fb63822bd51b032a": {
value: [
ObjectId("6255a889fb63822bd51b0321"),
ObjectId("6255a889fb63822bd51b032d")
],
},
"6255a889fb63822bd51b032e": {
value: 2,
comment: "hello",
}
},
createdAt: ISODate("2022-03-12T17:27:53.890Z")
}
]
我启动管道
{
$project: {
_id: 0,
answers: { $objectToArray: "$answers" },
date: { $dateToString: { format: dateFormat, date: "$createdAt" } },
},
},
并得到如下结果:
[
{
answers: [
{
k: '6255a9a60fd8f862f4bd287f',
v: {
value: new ObjectId("6255a9a60fd8f862f4bd2882"),
_id: new ObjectId("6255a9a60fd8f862f4bd28b2")
}
},
{
k: '6255a9a60fd8f862f4bd2883',
v: {
value: [
new ObjectId("6255a9a60fd8f862f4bd287a"),
new ObjectId("6255a9a60fd8f862f4bd2886")
],
_id: new ObjectId("6255a9a60fd8f862f4bd28b3")
}
},
{
k: '6255a9a60fd8f862f4bd2887',
v: {
value: 2,
comment: 'hello',
_id: new ObjectId("6255a9a60fd8f862f4bd28b4")
}
}
],
date: '2022-03-12'
}
]
我想拉起 value
并将 v
替换为 value< /code> 所以我最终得到的结果如下所示。我在想也许有某种方法可以在 $objectToArray 的第一步中做到这一点?或者之后我还需要采取其他步骤吗?
[
{
answers: [
{
k: '6255a9a60fd8f862f4bd287f',
v: new ObjectId("6255a9a60fd8f862f4bd2882")
},
{
k: '6255a9a60fd8f862f4bd2883',
v: [
new ObjectId("6255a9a60fd8f862f4bd287a"),
new ObjectId("6255a9a60fd8f862f4bd2886")
]
},
{
k: '6255a9a60fd8f862f4bd2887',
v: 2
}
],
date: '2022-03-12'
}
]
I have a collection that looks like this:
[
{
_id: ObjectId("6255a889fb63822bd51b0356"),
answers: {
"6255a889fb63822bd51b0326": {
value: ObjectId("6255a889fb63822bd51b0329"),
},
"6255a889fb63822bd51b032a": {
value: [
ObjectId("6255a889fb63822bd51b0321"),
ObjectId("6255a889fb63822bd51b032d")
],
},
"6255a889fb63822bd51b032e": {
value: 2,
comment: "hello",
}
},
createdAt: ISODate("2022-03-12T17:27:53.890Z")
}
]
I start my pipeline with
{
$project: {
_id: 0,
answers: { $objectToArray: "$answers" },
date: { $dateToString: { format: dateFormat, date: "$createdAt" } },
},
},
and get a result like this:
[
{
answers: [
{
k: '6255a9a60fd8f862f4bd287f',
v: {
value: new ObjectId("6255a9a60fd8f862f4bd2882"),
_id: new ObjectId("6255a9a60fd8f862f4bd28b2")
}
},
{
k: '6255a9a60fd8f862f4bd2883',
v: {
value: [
new ObjectId("6255a9a60fd8f862f4bd287a"),
new ObjectId("6255a9a60fd8f862f4bd2886")
],
_id: new ObjectId("6255a9a60fd8f862f4bd28b3")
}
},
{
k: '6255a9a60fd8f862f4bd2887',
v: {
value: 2,
comment: 'hello',
_id: new ObjectId("6255a9a60fd8f862f4bd28b4")
}
}
],
date: '2022-03-12'
}
]
I want to pull value
up and replace v
with value
so I wind up with a result that looks like this. I was thinking maybe there's some way to do it in the first step of $objectToArray? or do I need another step afterwards?
[
{
answers: [
{
k: '6255a9a60fd8f862f4bd287f',
v: new ObjectId("6255a9a60fd8f862f4bd2882")
},
{
k: '6255a9a60fd8f862f4bd2883',
v: [
new ObjectId("6255a9a60fd8f862f4bd287a"),
new ObjectId("6255a9a60fd8f862f4bd2886")
]
},
{
k: '6255a9a60fd8f862f4bd2887',
v: 2
}
],
date: '2022-03-12'
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要再一个
$map
来调整您想要的形式。这是 Mongo Playground 供您参考。
You just need one more
$map
to wrangle to your desired form.This is the Mongo playground for your reference.