How to insert multiple items in a MongoDB collection at once

I need to insert multiple items into one at a timeMongoDBCollections in Node.js applications

This is what I did:

const mongo = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017'
let db, jobs

mongo.connect( url, { useNewUrlParser: true, useUnifiedTopology: true, }, (err, client) => { if (err) { console.error(err) return } db = client.db(‘myapp’) jobs = db.collection(‘jobs’)

<span style="color:#66d9ef">const</span> <span style="color:#a6e22e">data</span> <span style="color:#f92672">=</span> [{...}, {...}]
<span style="color:#a6e22e">jobs</span>.<span style="color:#a6e22e">insertMany</span>(<span style="color:#a6e22e">data</span>)

} )

Given an array of objects, I callinsertMany()I want to fill the collection.


More database tutorials: