I am currently working with an array of strings and my goal is to iterate through this array and update my collection with its values.
This is the approach I have taken:
if (employees) {
employees.map((employee) => {
Employee.updateOne({ $push: { name: employee.name } })
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log(e);
});
});
}
First, I import my model at the top of the file :
const Employee = require('../../models/employees');
The structure of my model is as follows :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const EmployeeSchema = new Schema({
name: { type: String },
});
const Employee = mongoose.model('employee', EmployeeSchema);
module.exports = Employee;
Although my console is showing the following output:
{ n: 0, nModified: 0, ok: 1 }
When I check the database, no data is present and no collection is created as expected.