【问题标题】:How can I insert/update JSON array in Mongoose? [duplicate]如何在 Mongoose 中插入/更新 JSON 数组? [复制]
【发布时间】:2018-06-16 01:39:03
【问题描述】:

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

[
    {
        "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 数组?

【问题讨论】:

    标签: json node.js mongodb mongoose


    【解决方案1】:

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

    你可以添加一个 JSON 数组,Mongoose 会将它添加到它的集合中。

    【讨论】:

    • 我得到了这个错误:insertMany is not a function,你能通过完整的例子帮助我吗?
    • 假设您使用 Mongoose 定义了模型 Table。要插入一个表数组,请执行以下操作:Table.insertMany(your_JSON_array).then(doc => { // something... }).
    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 2018-01-02
    • 1970-01-01
    • 2018-02-05
    • 2020-12-17
    • 2017-05-20
    相关资源
    最近更新 更多