I am trying to update the documents with the new field name which needs to be combination of firstName and lastName from the same document. I have written a snippet here and trying to update values in the database. It's not getting updated, there are no errors. I have tried to cast _id to ObjectId, But it didn't work.
MongoDB: 3.4 Mongoose: 4.11.x
const mongoose = require('mongoose')
const User = require('./models/user')
mongoose.connect('mongodb://localhost:27017/db', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
})
const users = User.find({}, function(error, doc){
doc.forEach(function(raw_doc){
if(raw_doc.firstName) {
firstName = raw_doc.firstName
}else{
firstName = ''
}
if(raw_doc.lastName) {
lastName = raw_doc.lastName
}else{
lastName = ''
}
name = firstName + " " + lastName
const user_update = User.findByIdAndUpdate(raw_doc._id, { name: name }, function(error, res) {
if (error){
console.log(error)
}else{
console.log(res)
}
});
});
});
console.log(res)
? – whoami - fakeFaceTrueSoulconsole.log(res)
? – whoami - fakeFaceTrueSoul{new:true}
but it is not updating the value ofname
field. I have cross-checked in the database, But the field is not updated. – Viraj