Home > database >  Hie i am new to mongo db. I want add a new field to a single existing document. How can i achieve th
Hie i am new to mongo db. I want add a new field to a single existing document. How can i achieve th

Time:08-21

For example i have document like this

{_id:1,
 name:"John"         
}

After adding new field it should be like this

 {_id:1,
 name:"John",
 last_name:"doe"         
}

CodePudding user response:

You probably want to use the updateOne API with the $set update operator:

db.collection.updateOne({_id: 1}, {$set: {last_name: "doe"}});

You didn't mention which library you're using, so I cannot recommend a specific call, but I'd assume that something like this should usually be available.

  • Related