如何在Mongoose中插入/更新JSON数组?

时间:2018-01-06 12:10:17

标签: json node.js mongodb mongoose

我在API中有一个post方法req.body包含JSON,如下所示:

[
    {
        "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"
    }
]

如何在Node中插入或更新Mongoose?有没有办法批量插入这个JSON数组?

1 个答案:

答案 0 :(得分:1)

您可以查看insertMany() http://mongoosejs.com/docs/api.html#model_Model.insertMany

您可以添加JSON数组,Mongoose会将其添加到其集合中。