Uzair Ali
Uzair Ali

Reputation: 11

How to add a new column/key in mongodb at run time?

In sql we can new column by writing query: "ALTER TABLE Customers ADD Email varchar(255)"

In nodejs, How can we add new column/key in mongodb, without directly changing the existing Schema?

Upvotes: 0

Views: 1543

Answers (1)

ray
ray

Reputation: 15276

Just use $set to set the new field and insert value.

db.collection.update({},
{
  $set: {
    "Email": "newValue"
  }
},
{
  multi: true
})

multi: true is for updating multiple documents.

Upvotes: 1

Related Questions