8.8. Inserting documents into MongoDB: Inserting complex documents into MongoDB

Página 16

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'.

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.

Next page of the Free Ebook:

178.9. Inserting documents into MongoDB: Inserting multiple documents into 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