我在mongodb中有一个图像数组,我正在尝试更改数组的架构。现在,图像存储得像下面这样。
["https://images.freeimages.com/images/large-previews/e51/tokyo05-2-1447803.jpg","https://images.freeimages.com/images/large-previews/aae/lomo-spider-1386711.jpg","https://images.freeimages.com/images/large-previews/383/the-home-of-the-candle-1-1425911.jpg"]我想要的最后输出就像下面一样。
[
{
url:
"https://images.freeimages.com/images/large-previews/e51/tokyo05-2-1447803.jpg",
index: "1"
},
{
url:
"https://images.freeimages.com/images/large-previews/aae/lomo-spider-1386711.jpg",
index: "2"
},
{
url:
"https://images.freeimages.com/images/large-previews/383/the-home-of-the-candle-1-1425911.jpg",
index: "3"
},
]我怎么能在mongosh做这件事?
作为Python这样做,然后导入回mongodb会更容易吗?谢谢您抽时间见我!
发布于 2022-06-05 08:44:44
由于mongoDB版本4.2+,您可以从mongosh中执行以下操作:
db.collection.update({},
[
{
$addFields: {
x: {
"$map": {
"input": "$x",
"as": "y",
"in": {
url: "$$y",
index: {
$indexOfArray: [
"$x",
"$$y"
]
}
}
}
}
}
}
],
{
multi: true
})解释:
https://stackoverflow.com/questions/72501264
复制相似问题