MongoDB modifies and deletes instances of the document's domain properties

  • 2020-05-13 03:44:56
  • OfStack

Because the blog application used by this blog is developed at the same time, some properties that are not required are created dynamically during the development process

MongoDB is Schema free, and unlike relational databases where column properties are defined in tables rather than records, each document in MongoDB's collection can have its own distinct domain properties.

MongoDB USES db.collections.update to modify the domain properties of several documents in the collection, adding the domain with $set, and deleting the domain with $unset.

Deletes 1 field for all documents in the collection


db.posts.update({}, { $unset: { deleted_at: 1 } }, { multi: true })

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

db.categories.update({}, { $unset: { deleted_at: 1, desc: 1 } }, { multi: true })

Also delete and add domains at the same time

db.tags.update(
    {},
    { $unset: { deleted_at: 1 }, $set: { slug: 1, description: 1 } },
    { multi: true }
)


Related articles: