MongoDB shell:使用正则表达式更新属性更改现有属性
我需要改变这一点:MongoDB shell:使用正则表达式更新属性更改现有属性,mongodb,Mongodb,我需要改变这一点: userId: 'sdfsd' pictures: ['https://stackoverflow.com/questions/ask.jpg', 'https://stackoverflow.com/questions/ask2.jpg'] 为此: userId: 'sdfsd' pictures: ['ask.jpg', 'ask2.jpg'] 有没有办法只用一个命令就可以做到这一点?不需要为您的用例应用正则表达式 MongoDb方式(v4.2) 我们按https:/
userId: 'sdfsd'
pictures: ['https://stackoverflow.com/questions/ask.jpg', 'https://stackoverflow.com/questions/ask2.jpg']
为此:
userId: 'sdfsd'
pictures: ['ask.jpg', 'ask2.jpg']
有没有办法只用一个命令就可以做到这一点?不需要为您的用例应用正则表达式 MongoDb方式(v4.2) 我们按
https://stackoverflow.com/questions/
或/
并取最后一项
db.collection.update({},
[
{
$set: {
pictures: {
$map: {
input: "$pictures",
in: {
$arrayElemAt: [
{
$split: [
"$$this",
"https://stackoverflow.com/questions/" // or split by "/"
]
},
-1
]
}
}
}
}
}
],
{
multi: true
})
JS方式
我们可以应用正则表达式、拆分等。。。并单独保存
db.collection.find({}).forEach(function(doc){
doc.pictures = doc.pictures.map(x => x.replace("https://stackoverflow.com/questions/", ""));
db.collection.save(doc);
})
MongoDB版本?