简体   繁体   English

使用ecmascript 6为数组的每个元素保存新的猫鼬模式

[英]save new mongoose schema for each element of array using ecmascript 6

I am looping through an array and creating a new mongoose schema, adding two fields, and then saving it. 我遍历一个数组并创建一个新的猫鼬模式,添加两个字段,然后保存它。

for (var i = 0; i < myArray.length; i++) {
    var newUsers = new UserList({
        email: myArray[i],
        uuid: uuidv4()
    });
    UserList.save(function (err) {
        if (err) console.log(err)
    });
}

Question: 题:

how can I achieve this using ecmaScript 6 best practices? 如何使用ecmaScript 6最佳实践来实现这一目标?

You can achieve your solution by using map method and mongoose insertMany method. 您可以使用map方法和mongoose insertMany方法来实现您的解决方案。

const bulkData = myArray.map(email => new UserList({ email, uuid: uuidv4()}));
UserList.insertMany(bulkData, (error, docs) => {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM