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: Inserting complex documents into MongoDB

Capítulo 16

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Inserting documents into MongoDB is a fundamental process for the operation of any data-driven application. Documents are the equivalent of records or rows in relational databases. In MongoDB, documents are data structures similar to JSON (JavaScript Object Notation) that can contain many different types of data and even other documents, creating complex data structures.

To insert documents into MongoDB, you will use the 'insertOne()' or 'insertMany()' method. The 'insertOne()' method is used to insert a single document into the database. For example:

db.collection('nomeOfCollection').insertOne({
  name: 'John',
  age: 30,
  profession: 'Engineer'
})

In the example above, a document is inserted into the collection 'nomeOfCollection'. The document contains three fields: 'name', 'age' and 'profession'. The 'name' value is a string, the 'age' value is a number and the 'profession' value is also a string.

The 'insertMany()' method is used to insert multiple documents at once. Documents are provided as an array of objects. For example:

db.collection('nomeOfCollection').insertMany([
  { name: 'Maria', age: 25, profession: 'Doctor' },
  { name: 'Pedro', age: 40, profession: 'Lawyer' },
  { name: 'Ana', age: 35, profession: 'Teacher' }
])

In the example above, three documents are inserted into the 'nomeOfCollection' collection. Each document is an object with fields 'name', 'age' and 'profession'.

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

In addition to inserting simple documents like the examples above, MongoDB also allows the insertion of complex documents that contain arrays and subdocuments.

To insert an array into a document, you simply provide the array as a value for a field. For example:

db.collection('nomeOfCollection').insertOne({
  name: 'John',
  age: 30,
  profession: 'Engineer',
  skills: ['Math', 'Physics', 'Programming']
})

In the example above, the document contains a 'skills' field whose value is an array of strings.

To insert a subdocument, you provide an object as a value for a field. For example:

db.collection('nomeOfCollection').insertOne({
  name: 'John',
  age: 30,
  profession: 'Engineer',
  address: {
    street: 'Rua das Flores',
    number: 123,
    Sao Paulo city',
    status: 'SP'
  }
})

In the example above, the document contains a field 'address' whose value is a subdocument. The subdocument has its own fields: 'street', 'number', 'city' and 'state'.

With the ability to insert complex documents, MongoDB offers great flexibility in modeling your data. You can create data structures that perfectly fit your needs, without the need to conform to the constraints of a rigid schema like in relational databases.

It is important to note that although MongoDB allows the creation of complex documents, it is good practice to keep your documents as simple as possible. Complex documents can be more difficult to understand and manipulate, and can lead to lower performance if used imprudently.

In summary, inserting documents into MongoDB is a simple but powerful process. With the ability to insert simple or complex documents, you have the flexibility to shape your data according to your specific needs.

Now answer the exercise about the content:

Which of the following methods is used to insert multiple documents at once into MongoDB?

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

You missed! Try again.

The method used to insert multiple documents at once into MongoDB is insertMany(). This method allows batching multiple documents together for insertion, making it more efficient for bulk operations compared to inserting documents one by one with insertOne().

Next chapter

Inserting documents into MongoDB: Inserting multiple documents into MongoDB

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