How to save an array of objects to mongoose DB with only one call?

后端 未结 5 1711
野的像风
野的像风 2020-12-29 07:34

Is there any way to save an array of JSON object to a mongodb with only one call? something like:

schemeObject.save(array_of_json_object, ca         


        
5条回答
  •  梦谈多话
    2020-12-29 07:41

    How about something like this? I know it loops through the entire array.. dont ask me about the Big O for this.. probably not the best way to insert but works for just an initial data dump of some sort..

    var arr = // array of objects;
        res = [];
    
    arr.forEach(function (item) {
        item.save(function (err) {
            res.push(err);
            if (res.length === arr.length)
            {
                // Done
            }
        });
    });
    

提交回复
热议问题