8.9. Inserting documents into MongoDB: Inserting multiple documents into MongoDB

Página 17

MongoDB is an open source NoSQL database that supports different forms of data. One of its most notable features is the ability to insert multiple documents at once. This can be especially useful when you need to fill your database with many records at once. In this chapter, we will explore how to insert multiple documents into MongoDB.

To start, we need to understand how documents are structured in MongoDB. Unlike relational databases, where data is stored in tables, in MongoDB data is stored in collections. Each document within a collection is a JSON object, which can contain multiple key and value pairs.

To insert multiple documents, you can use the `insertMany()` method. This method accepts an array of objects and inserts all these objects into the specified collection. Let's look at an example. Suppose we have a collection called `users` and we want to insert three new users. Here is how you can do that:

```javascript db.users.insertMany([ { name: 'Alice', age: 25, email: '[email protected]' }, { name: 'Bob', age: 30, email: '[email protected]' }, { name: 'Charlie', age: 35, email: '[email protected]' } ]) ```

After executing this command, MongoDB will insert the three documents into the `users` collection. If the operation is successful, MongoDB returns an object that contains the status of the operation and the `_id`s of the inserted documents.

It's important to note that MongoDB automatically generates an `_id` for each document you insert unless you specify one. The `_id` is a unique identifier for each document, and MongoDB guarantees that within a collection, no two documents can exist with the same `_id`.

If you try to insert multiple documents with the same `_id`, MongoDB will throw an error and abort the insert operation. None of the documents will be inserted. Therefore, it is important to ensure that each document has a unique `_id`.

The `insertMany()` method also accepts an optional second argument, which is an options object. This object can contain several options, such as `ordered` and `writeConcern`. The `ordered` option is a boolean that determines whether MongoDB should insert documents in the order they appear in the array. If `ordered` is `true` (the default), MongoDB will stop the insert operation as soon as it encounters an error (such as a duplicate `_id`). If `ordered` is `false`, MongoDB will attempt to insert all documents, even if it encounters errors.

The `writeConcern` option controls MongoDB's write guarantee. You can specify the number of instances that must commit the insert operation before it is considered complete. For example, if you have a cluster of three MongoDB instances, you can set `writeConcern` to `2` to ensure that at least two instances commit the operation.

In summary, inserting multiple documents into MongoDB is a simple but powerful task. With the `insertMany()` method, you can quickly fill your database with many records. However, it is important to be aware of the implications of inserting documents with the same `_id` and how the `ordered` and `writeConcern` options can affect the insertion operation.

Now answer the exercise about the content:

What is the function of the `insertMany()` method in MongoDB and how does it work?

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

You missed! Try again.

Next page of the Free Ebook:

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

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