Free Ebook cover Creation and maintenance of MongoDB database from basic to advanced

Creation and maintenance of MongoDB database from basic to advanced

5

(1)

88 pages

Inserting documents into MongoDB: Using the $set operator to insert fields into documents

Capítulo 18

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

In the MongoDB learning journey, an essential skill is document insertion. Documents, which are equivalent to rows in a relational database, are the basic unit of data in MongoDB. Each document is a data structure that contains pairs of fields and values. Field values ​​can include other documents, arrays, and document arrays.

To insert documents into MongoDB, we use the insert() or insertOne() and insertMany() method. The insertOne() method is used when you want to insert a single document, while the insertMany() is used when you want to insert multiple documents at once. For example, to insert a document into the 'students' collection, you can use the following command:

    db.students.insertOne(
    {
        name: "John Doe",
        age: 21,
        subjects: ["Math", "Physics", "Computer Science"]
    })

Here, a document with fields 'name', 'age' and 'subjects' is inserted into the 'students' collection. If the operation is successful, MongoDB returns a result object that includes the ID of the inserted document.

Sometimes you may need to add additional fields to a document after it is created. For this, MongoDB provides the $set operator. The $set operator is used to add new fields to a document or to update the values ​​of existing fields. The $set operator has the following syntax:

    db.collection.update(query, { $set: { : , ... } })

Where 'query' is the condition that must be met, 'field1' is the name of the field you want to add or update, and 'value1' is the new value you want to set for the field. For example, to add a new field 'graduated' to the document for student 'John Doe', you can use the following command:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

    db.students.update({ name: "John Doe" }, { $set: { graduated: false } })

Here, the $set operator adds the field 'graduated' with the value 'false' to the student document 'John Doe'. If the 'graduated' field already exists in the document, its value will be updated to 'false'. If the operation is successful, MongoDB returns a result object that includes the number of modified documents.

In addition, the $set operator can be used in combination with other operators to perform more complex updates. For example, you can use the $set operator in combination with the $inc operator to increment the value of a field and add a new field at the same time. The $inc operator increments the value of a field by a specified amount. For example, to increment the age of student 'John Doe' and add a 'graduated' field, you can use the following command:

    db.students.update({ name: "John Doe" }, { $inc: { age: 1 }, $set: { graduated: false } })

Here, the $inc operator increments the age of the student 'John Doe' by 1 and the $set operator adds the 'graduated' field with the value 'false '.

In summary, inserting documents and adding fields to documents are fundamental operations in MongoDB. The insert() method is used to insert documents and the $set operator is used to add fields to documents. Additionally, the $set operator can be used in combination with other operators to perform more complex updates.

Now answer the exercise about the content:

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

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

You missed! Try again.

The $set operator in MongoDB is used to add new fields to a document or update the values of existing fields, as described in the text. It allows modification of documents after their creation, enhancing flexibility in data management.

Next chapter

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

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.