Haris KK
Haris KK

Reputation: 267

How can i remove a specified field from all documents of a collection using mongoose?

I want to remove key "passwrod" from all documents from users collection using mongoose , is it possible to do it using $unset ?

 { "_id" : ObjectId("58ec890c91b2b612084fd827"),
        "username" : "zain",
        "passwrod" : 123,
        "password" : 8 },
{   "_id" : ObjectId("58ec8918364116187845948d"),
        "username" : "bob",
        "password" : 123,
        "passwrod" : 12  }

Upvotes: 8

Views: 12785

Answers (1)

AshokGK
AshokGK

Reputation: 875

Documents:

{ "_id" : ObjectId("58ec890c91b2b612084fd827"), "username" : "zain", "passwrod" : 123, "password" : 8 }
{ "_id" : ObjectId("58ec8918364116187845948d"), "username" : "bob", "password" : 123, "passwrod" : 12 }

Query:

db.collection.updateMany({}, {$unset:{"passwrod":1}})

Result:

{ "_id" : ObjectId("58ec890c91b2b612084fd827"), "username" : "zain", "password" : 8 }
{ "_id" : ObjectId("58ec8918364116187845948d"), "username" : "bob", "password" : 123 }

Upvotes: 10

Related Questions