How to post/inesrt many documents at once in mongodb using express?

Viewed 21

I used this command but it's not working!. I want to insert many documents at once in mongodb using express js ...

app.post("/products", async (req, res) => {
      const body = req.body
      const docs = [{ body:body }] 
      console.log(docs);
      const options = { ordered: true }
      const result = await productCollection.insertMany(docs, options)
      res.send(result)
})

It's giving the error below:

callback(new error_1.MongoServerError(document));
BSON field 'insert.documents.0' is the wrong type 'array', expected type 'object'
1 Answers

Look at productCollection schema model - looks like you set it to Object while trying to insert an Array.

Related