具有以下结构的数据库:
_id:5d0fe110d7b8c01a4c633222
Category:"Stripveiling (Nederlands)"
Lot title:"Blake en Mortimer - S.O.S. Meteoren - 1e Druk HC 1959"
Seller name:"Stripsmagazijn"
Seller country:"Nederland"
Bids count:22
Winning bid:"€ 1.950"
Bid amount:"Closed"
我需要将“中标”的值从“ 1.950”更改为“ 1950”。可能有很多含义,它们可能有所不同,因此$ replaceOne()不适合。有人可以帮忙吗?
继续上一个帖子的答案。
我认为您可以在阶段中使用$ split和$ reduce作为管道的最后阶段来转换数据库中所有此类事件。$project
$out
这个想法是将split
带有"."
和的字符串concat
返回数组以形成一个字符串,但是不带"."
,然后可以继续正常过程。
$out failed: { ok: 0.0, errmsg: "operation exceeded time limit", code: 50, codeName: "MaxTimeMSExpired" }
由于超时而收到错误,您可以使用$ maxTimeMS增加默认超时
db.collection_name.aggregate([
{
$project: {
category : "$category",
category_name : "$category_name",
lot_title : "$lot_title",
seller_name : "$seller_name",
seller_country : "$seller_country",
bid_count : "$bid_count",
winning_bid : {
$toInt : {
$substr : [
{
$reduce : {
input : { $split : ["$winning_bid","."]}},
initialValue: "",
in: { $concat : ["$$value", "$$this"] }
}
},
2,
-1
]
}
},
bid_amount : "$bid_amount",
lot_image : "$lot_image"
}
},{
$out : "collection_name"
}
]).maxTimeMS(100)
您可以根据需要增加超时时间。
我还没有测试过代码,它在理论上应该可以工作,但是您知道了,可以更改代码以适合您的需要。
错误:无法从BSON类型数组转换为String
哦,是的,$ concatArray concats数组,我将更新答案
我找到了一个解决方案:winning_bid:{$ toInt:{$ arrayElemAt:[{$ split:[{$ trim:{input:“ $ Winning bid”,字符:“€”}},“。”]},0] }}
但是我有一些奇怪的错误:为$ out插入失败:{好的:0.0,errmsg:“操作超出了时间限制”,代码:50,codeName:“ MaxTimeMSExpired”}
是的,那是因为超时错误,请使用$ maxTimeMS增加超时时间。