SoftTimur
SoftTimur

Reputation: 5510

Delete a key in all the documents

I have modified a schema (ie, users) by deleting a key (ie, ips). Thus I want to delete this key in all the documents in the database.

For example, in mongo console or Robo 3T, db.getCollection('users').find({}) returns all the users. Some of them contain the key ips. Does anyone know how to remove ips in the console or Robo 3T?

Upvotes: 0

Views: 303

Answers (2)

codejockie
codejockie

Reputation: 10864

Update Multiple Documents

To update multiple documents, set the multi option to true. Refer here

db.getCollection('users').update(
  { },
  { $unset: { ips: 1 } },
  { multi: true } 
)

Upvotes: 2

Zilvinas
Zilvinas

Reputation: 5578

As @Veeram already posted you can run a regular update with $unset just add multi: true in the options to update all documents, otherwise it will update just one

db.users.update(
  { }, // where
  { $unset: { ips: 1 } }, // change what
  { multi: true } // options
)

Upvotes: 1

Related Questions