How add new attribute to existing records

I have an existing database with the following schema

let Employee = new Schema({
first_name: { type: String },
last_name: { type: String },
title: { type: String },
email: { type: String },
gender: { type: String },
location: { type: String },
phone: { type: Number }
as I already have many records present in my DB, I want to add a new attribute branch to my database. Is there any function for the same regard.
I tried the following statement, but got an error
db.Employees.updateMany({$set:{branch:'Islamabad'}})

Don’t know if its the correct forum to post or not, but thanks in advance to helping hands

Hey @Ahsan_72923

Are you using Mongoose.js? If so probably not the right forum. However you will should to add the new field branch to your Mongoose Model Schema first before running your update command. Or you Mongoose Model will never return the new value.

And you have the wrong syntax for the updateMany command see the docs below
https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/index.html#db.collection.updateMany

1 Like