How to remove a field completely from a MongoDB document?

Technology CommunityCategory: MongoDBHow to remove a field completely from a MongoDB document?
VietMX Staff asked 3 years ago

Suppose this is a document.

{ 
    name: 'book',
    tags: {
        words: ['abc','123'], // <-- remove it comletely
        lat: 33,
        long: 22
    }
}

How do I remove words completely from all the documents in this collection?

Answer

db.example.update({}, {$unset: {words:1}}, false, true);