0

I have a post method in an API that req.body contains JSON like this:

[
    {
        "art": "A",
        "count": "20",
        "name": "name1",
        "ean": "802.0079.127",
        "marker": "null",
        "stammkost": "A"
    },
    {
        "art": "A",
        "count": "10",
        "name": "name2",
        "ean": "657.7406.559",
        "marker": "null",
        "stammkost": "A"
    }
]

How can I insert or update Mongoose in Node? Is there any way to bulk insert this JSON array?

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
Hamed Rahmani
  • 41
  • 1
  • 4
  • 10

1 Answers1

2

You can look at insertMany() http://mongoosejs.com/docs/api.html#model_Model.insertMany

You can add a JSON array, and Mongoose will add it to its collection.

Pierre R-A
  • 509
  • 9
  • 13
  • i get this erorr: `insertMany is not a function`, could you help me by complete example please ? – Hamed Rahmani Jan 06 '18 at 13:26
  • 1
    Say you have the model `Table` defined with Mongoose. You want to insert an array of tables, you do as follow: `Table.insertMany(your_JSON_array).then(doc => { // something... })`. – Pierre R-A Jan 06 '18 at 16:58