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)
});
}
how can I achieve this using ecmaScript 6 best practices?
You can achieve your solution by using map method and mongoose insertMany method.
const bulkData = myArray.map(email => new UserList({ email, uuid: uuidv4()}));
UserList.insertMany(bulkData, (error, docs) => {});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.