Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing MongoDB Chapter 3

The insertOne() and insertMany() commands only work for Mongo
version 3.2 or higher.

insertMany()

This command is used for inserting multiple documents into a collection. In the preceding


example, we saw that the insertOne() command takes an argument that is an object.


The insertMany() command takes an array as a parameter so that we can pass multiple


objects inside it and insert multiple documents in the collection. Let's look at an example:


> db.users.insertMany(
[
{ name: "Jack", email: "[email protected]" },
{ name: "John", email: "[email protected]" },
{ name: "Peter", email: "[email protected]" }
]
)

This snippet creates three documents in the users collection. When we run the command,


the output should be:

Free download pdf