Mongoose create multiple documents

后端 未结 4 923
粉色の甜心
粉色の甜心 2020-12-29 19:39

I know in the latest version of Mongoose you can pass multiple documents to the create method, or even better in my case an array of documents.

var array = [         


        
4条回答
  •  醉话见心
    2020-12-29 20:18

    With Mongoose v5.1.5, we can use insertMany() method with array passed.

    const array = [
        {firstName: "Jelly", lastName: "Bean"},
        {firstName: "John", lastName: "Doe"}
    ];
    
    Model.insertMany(array)
        .then(function (docs) {
            response.json(docs);
        })
        .catch(function (err) {
            response.status(500).send(err);
        });
    

提交回复
热议问题