我想searchname根據當前存在的所有檔案添加一個新欄位,name但采用名稱中的字串并將所有 é 替換為常規 e。我如何在 shell 或驅動程式中執行此操作。我正在 pymongo 中嘗試這個,但我收到錯誤 ```Invalid $set :: 由 :: 表示一個運算式的物件必須有一個欄位``
這是我試圖運行的代碼
test_collection.update_many({},
[{
"$set": {
"searchName": {
"$reduce": {
"input": [
["é", "e"],
["à", "a"],
["í", "i"],
["ó", "o"],
["ú", "u"],
["?", "n"],
],
"initialValue": {
"$toLower": "$name"
},
"in": {
"$replaceAll": {
"input": "$$value",
"find": {
"$arrayElemAt": [
"$$this",
0
]
}
},
"replacement": {
"$arrayElemAt": [
"$$this",
1
]
}
}}}}}])
uj5u.com熱心網友回復:
查詢1
- 管道更新需要 MongoDB >= 4.2
- 您可以使用
$replaceAll聚合運算子 - 僅限 1 個字符
測驗代碼在這里
update({},
[
{
"$set": {
"searchName": {
"$replaceAll": {
"input": "$name",
"find": "é",
"replacement": "e"
}
}
}
}
],
{"multi": true})
查詢2
- 用它來替換許多字符
- 將您的字符/替換放在“輸入”中
測驗代碼在這里
update({},
[{"$set":
{"searchName":
{"$reduce":
{"input": [["é", "e"], ["l", "k"]],
"initialValue": "$name",
"in":
{"$replaceAll":
{"input": "$$value",
"find": {"$arrayElemAt": ["$$this", 0]},
"replacement": {"$arrayElemAt": ["$$this", 1]}}}}}}}],
{"multi": true})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359651.html
標籤:MongoDB mongodb-查询 皮蒙戈
