# insertOne & insertMany — MongoDB

Source: https://www.geekswithgeeks.com/en/mongodb/mongo-insert

> Add one or many documents to a collection.

## insertOne()

Inserts a single document into a collection, creating the collection if it doesn't exist yet.

```javascript
db.users.insertOne({
  name: "Grace Hopper",
  role: "engineer"
})
```

Output:

```
{
  acknowledged: true,
  insertedId: ObjectId("65f1a2b3c4d5e6f7a8b9c0d2")
}
```

## insertMany()

Pass an array of documents to insert several at once.

```javascript
db.users.insertMany([
  { name: "Alan Turing", role: "mathematician" },
  { name: "Barbara Liskov", role: "engineer" }
])
```

Output:

```
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("...d3"),
    '1': ObjectId("...d4")
  }
}
```

## Supplying your own _id

You can set `_id` yourself (e.g. a string or number) as long as it's unique within the collection.
