8.11. Inserting documents into MongoDB: Using the $unset operator to remove document fields

Página 19

Inserting documents into MongoDB: Using the $unset operator to remove document fields

Inserting documents into MongoDB: Using the $unset operator to remove document fields

MongoDB is an open-source NoSQL database that provides support for different types of data formats such as documents, graphs, key/value, and others. It uses BSON (Binary JSON) format to store data. One of the most powerful features of MongoDB is the ability to modify existing documents using update operators.

One of these update operators is the $unset operator. This operator is used to remove a specified field from a document. The $unset operation removes the field and its value from the document. If the specified field does not exist in the document, the $unset operation has no effect.

Using the $unset operator

To use the $unset operator, you need to specify the name of the field you want to remove from the document. The basic syntax of the $unset operator is as follows:

        
            { $unset: { : "", ... } }
        
    

For example, if you have a document like this:

        
            {
                "_id" : ObjectId("5f1b402b3a682a2e144e6e0d"),
                "name" : "John",
                "age" : 25,
                "profession": "Engineer"
            }
        
    

If you want to remove the "profession" field, you can use the $unset operator as follows:

        
            db.collection.update(
                { "name" : "John" },
                { $unset: { "profession": "" } }
            )
        
    

After executing this operation, the document will be modified to:

        
            {
                "_id" : ObjectId("5f1b402b3a682a2e144e6e0d"),
                "name" : "John",
                "age": 25
            }
        
    

Removing multiple fields

The $unset operator can also be used to remove multiple fields from a document at once. To do this, you need to specify the names of the fields you want to remove in the $unset statement. The syntax for removing multiple fields is as follows:

        
            { $unset: { : "", : "", ... } }
        
    

For example, if you want to remove the "age" and "occupation" fields from the previous document, you can use the $unset operator as follows:

        
            db.collection.update(
                { "name" : "John" },
                { $unset: { "age": "", "profession": "" } }
            )
        
    

After executing this operation, the document will be modified to:

        
            {
                "_id" : ObjectId("5f1b402b3a682a2e144e6e0d"),
                "name": "John"
            }
        
    

Conclusion

The $unset operator is a powerful tool that allows you to modify existing documents in MongoDB. It allows you to remove one or more fields from a document at once, which can be very useful for keeping the structure of your documents clean and organized. However, it is important to remember that the $unset operator permanently removes the field and its value, so make sure you really want to remove these fields before using this operator.

Now answer the exercise about the content:

What is the function of the $unset operator in MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

208.12. Inserting documents into MongoDB: Validating documents in MongoDB

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text