我正在嘗試將 mongo 集合中當前設定為 Int64 的欄位的所有檔案轉換為 Int32。
目前這里是我的檔案:
{
_id: ObjectId("60af668346895440fad766c2"),
userId: 'xxxxxxxx',
postName: 'testPost',
numberOfComments: Long("3"),
numberOfLikes: Long("6")
}
如上所述,我想將 numerbOfComments 從 Long (Int64) 更改為 Int32
我嘗試過使用多種方法(見下文),但它只是更改了輸出中的資料型別,并沒有修改資料庫上的檔案。
任何人都可以幫忙嗎
到目前為止我嘗試過的
db.posts.aggregate(
[
{
$project:
{
result:
{
$convert: {
input: "$numberOfComments",
to: "int",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
)
我也試過這個:
db.posts.aggregate([
{
$set: {
numberOfComments: { toInt: '$numberOfComments' },
},
},
], {multi: true})
db.posts.updateMany(
{ numberOfComments : { $type: Long } },
[{ $set: { numberOfComments: { $toInt: "$numberOfComments" } } }],
{ multi: true })
db.UserProjects.aggregate(
[
{
$project:
{
_id: 0,
numberOfComments: { $toInt: "$numberOfComments" }
}
}
]
).pretty()
uj5u.com熱心網友回復:
您需要使用聚合更新。
操場
db.collection.update({
key: 3 //You can leave empty
},
[ //aggregate update as you need $toInt
{
$set: {
numberOfComments: {
"$toInt": "$numberOfComments"
}
}
}
])
我在操場上設定了一個錯誤示例。如果您設定了一個有效值,它將起作用。
請探索選項部分以更新一個/多個檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/422864.html
標籤:
