Arrays 删除mongodb阵列的一部分
嗨,我正在尝试删除第一个值之后的任何内容,例如 收藏:{Arrays 删除mongodb阵列的一部分,arrays,mongodb,mongodb-query,aggregation-framework,Arrays,Mongodb,Mongodb Query,Aggregation Framework,嗨,我正在尝试删除第一个值之后的任何内容,例如 收藏:{ "_id' :..... "UrlId" : "5dfc1aa2986b7c30f3398be4", "coverInput" : [ "https://test.com/s/files/1/00351576213050", "https://test.com.au" ], "_id' :..... "UrlId" : "5dfc1aa
"_id' :.....
"UrlId" : "5dfc1aa2986b7c30f3398be4",
"coverInput" :
[
"https://test.com/s/files/1/00351576213050",
"https://test.com.au"
],
"_id' :.....
"UrlId" : "5dfc1aa2986b7c30f3398be4",
"coverInput" :
[
"https://test.com/s/files/1/00351576213050"
],
}
我想用以下内容替换该文档:
收藏:{
"_id' :.....
"UrlId" : "5dfc1aa2986b7c30f3398be4",
"coverInput" :
[
"https://test.com/s/files/1/00351576213050",
"https://test.com.au"
],
"_id' :.....
"UrlId" : "5dfc1aa2986b7c30f3398be4",
"coverInput" :
[
"https://test.com/s/files/1/00351576213050"
],
}
我相信这很简单。我尝试了这个,但出现了一个错误:
db.test.aggregate([
{
$addFields: {
coverInput: { $substr: [ "$coverInput", 0, { $indexOfBytes: [ "$coverInput", "," ] } ] }
}
}
]).forEach( doc => db.test.updateOne( { _id: doc._id }, { $set: { coverInput: doc.coverInput } } ) )
提前谢谢 您可以使用下面的聚合
db.collection.aggregate([
{ "$project": {
"coverInput": {
"$slice": ["$coverInput", 1]
}
}}
])
或者如果要更新文档
db.test.updateOne(
{ _id: doc._id },
[{
$set: { coverInput: { "$slice": ["$coverInput", 1] } }
}]
)
$substr
运算符用于处理字符串。必须使用聚合数组运算符来处理数组字段。