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

Página 18

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:

    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.

Next page of the Free Ebook:

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

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