Reputation: 53
Can you remove a field from all collections in mongodb? I explain myself, let's think i have 3 databases called db1, db2 and db3. And random documents in them with fields and data. Is there any command to remove certain field from all of that databases or you can just remove a field from all documents?
Upvotes: 0
Views: 68
Reputation: 1086
Suppose you wanted to remove field1
from all documents
You'll have to loop through db1
, db2
and their respective collections,
Then use $unset
to remove the field
var dbList = ["db1", "db2"]
var mongo = new Mongo()
dbList.forEach(function(dbName){
var db = mongo.getDB(dbName)
print("Removing from db: "+dbName)
db.getCollectionNames().forEach(function(collName){
print("\tRemoving from collection: "+collName)
db.getCollection(collName).updateMany({}, {$unset:{"field1":""}})
})
})
Upvotes: 1