我有這樣的架構
const vocabularySchema = new mongoose.Schema({
vocabulary: { type: String, required: true },
defination: { type: String, required: true },
exampleSentences: [{ type: String }],
note: { type: String },
timeStamp: { type: Date, default: Date.now },
resource: { type: String },
owner: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
});
查看“定義”鍵。拼寫不正確。我想把它改回“定義”。但問題是有很多檔案添加了“定義”。我如何將它們改回“定義”?
更清楚地說,我將這些資料添加了關鍵的“定義”。他們如下
[
{
_id: new ObjectId("618fe4f6ee433e36f0392785"),
vocabulary: 'destiny',
defination: 'the events that will necessarily happen to a particular person or thing in the future.',
exampleSentences: [ 'If something is our destiny, there is no way we can avoid it.' ],
note: '',
timeStamp: 2021-11-13T16:16:54.897Z,
resource: '',
owner: new ObjectId("6162e68db8d492f28b8e7870"),
__v: 0
}
{
_id: new ObjectId("618fe554ee433e36f0392795"),
vocabulary: 'rumor',
defination: 'a currently circulating story or report of uncertain or doubtful truth.',
exampleSentences: [ 'You should never be bothered by rumor s.' ],
note: '',
timeStamp: 2021-11-13T16:18:28.523Z,
resource: '',
owner: new ObjectId("6162e68db8d492f28b8e7870"),
__v: 0
}
]
我想將這些現有檔案中的關鍵“定義”更改為“定義”。這是我迄今為止嘗試過但沒有奏效的方法。
Vocabulary.find({}).then((voca) => {
voca.forEach((v) => {
v["definition"] = v["defination"];
console.log(v);
});
});
請幫助我如何做到這一點。
uj5u.com熱心網友回復:
您可以使用$rename運算子。首先過濾所有具有defination欄位的檔案,然后為所有這些檔案重命名該欄位。
Vocabulary.update(
{ defination: { $exists: true } },
{ $rename: { "defination": "definition" } },
{ upsert: false, multi: true }
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357793.html
