MongoDB to modify, delete a document's Domain property instance _mongodb

Source: Internet
Author: User
Tags mongodb mongodb collection

Since the blog program used by this blog is used by Edge development, some properties that are not needed are created dynamically during the development process.

MongoDB is Schema-free, unlike relational databases, where column properties are defined in tables rather than records, each document in a MongoDB collection can have different field properties.

MongoDB use Db.collections.update to modify the field properties of several documents in the collection, use $set to add fields, $unset delete the fields.

Deletes a field for all documents in the collection

Copy Code code as follows:

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

1. The first parameter indicates that some documents are selected, where {} indicates that all documents in the current posts collection are selected
2. The second parameter is a specific update operation, $unset represents the deletion of the domain
3. The third parameter is an additional option, {Multi:true} represents updating all documents that meet the requirements, and defaults to updating only the first
You can also delete multiple domains at the same time
Copy Code code as follows:

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

also delete and add fields at the same time
Copy Code code as follows:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.