1

I have a schema like this

     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" },
    });

look at the "defination" key. The spelling is incorrect. I want to change it back to "definition". But the problem is there are a lot of documents added with "defination". How do I change them back to "definition"?

To be more clear, I have these data added with key "defination". They are as follow

    [
     {
      _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
    }
    ]

I want to change the key "defination" to "definition" in these existing documents. This is what I have tried so far but didn't work.

    Vocabulary.find({}).then((voca) => {
      voca.forEach((v) => {
        v["definition"] = v["defination"];
        console.log(v);
      });
    });

Please help me with how to do that.

NeNaD
  • 18,172
  • 8
  • 47
  • 89
Nyi Nyi Hmue Aung
  • 587
  • 1
  • 7
  • 19
  • Just looking at your last piece of code - shouldn't the assignment be the other way round? – Radoslaw Dubiel Nov 14 '21 at 09:22
  • Could you please be more precise? I don't understand what you mean. – Nyi Nyi Hmue Aung Nov 14 '21 at 09:24
  • Nevermind, it is me who didn't do the research, sorry. Take a look here, it covers your problem :) https://docs.mongodb.com/manual/reference/operator/update/rename/ – Radoslaw Dubiel Nov 14 '21 at 09:28
  • I guess it's supposed to be v["definition"] =v["defination"] since I want new key with "definition" and they value from "defination". I found it here https://stackoverflow.com/questions/4647817/javascript-object-rename-key . Thanks for the doc link. I will read it. – Nyi Nyi Hmue Aung Nov 14 '21 at 09:30

1 Answers1

1

You can use $rename operator. First filter all documents that have defination field, and then rename that field for all these documents.

Vocabulary.update(
  { defination: { $exists: true } },
  { $rename: { "defination": "definition" } },
  { upsert: false, multi: true  }
);
NeNaD
  • 18,172
  • 8
  • 47
  • 89