Mongodb modifies and deletes domain attribute instances of documents

Time:2020-3-1

Because the blog program used in this blog is developed and used at the same time, some properties that are not needed at present are created dynamically during the development process

Mongodb is schema free. Unlike relational databases, where column attributes are defined in tables rather than records, each document in mongodb‘s collection can have its own domain attributes.

In mongodb, db.collections.update is used to modify the domain properties of several documents in the collection. Use $set to add the domain and $unset to delete the domain.

Delete a field for all documents in the collection

Copy codeThe code is as follows:
db.posts.update({}, { $unset: { deleted_at: 1 } }, { multi: true })

1. The first parameter indicates that some documents are selected. Here, {} indicates that all documents in the current posts collection are selected
2. The second parameter is the specific update operation, $unset means delete domain
3. The third parameter is an extra option, {multi: true} means to update all documents that meet the requirements. By default, only the first one will be updated
You can also delete multiple domains at the same time

Copy codeThe code is as follows:
db.categories.update({}, { $unset: { deleted_at: 1, desc: 1 } }, { multi: true })

Also delete and add domains at the same time

Copy codeThe code is as follows:
db.tags.update(
    {},
    { $unset: { deleted_at: 1 }, $set: { slug: 1, description: 1 } },
    { multi: true }
)