Answer the question
In order to leave comments, you need to log in
Update in mongoose not updating fields?
I have a model:
const mongoose = require('mongoose');
let categorySchema = mongoose.Schema({
price: Number,
square: Number,
prestige: Number,
ecoSituation: String,
transport: Array,
typeOfHouse: String,
numberOfRooms: Number,
updated: Date,
upgraded: Date
});
let apartment = mongoose.model('apartments', categorySchema);
module.exports = apartment;
const apartment = require('../models/apartment');
class AddNewCategory {
constructor() {}
newCategory(req, callback) {
let catName = req.body.name;
let default_ = req.body.default;
apartment.where()
.setOptions({ multi: true })
.update({ $set: { 'ar': 'asd' }}, (err, result) => {
if(err) {
console.error("err => " + err);
} else {
console.log(result);
callback();
}
});
apartment.findOne({}, (err, apart) => {
apartment.update(apart, {$set: {arrr: 'asd'}}, (err, result) => {
if(err) {
console.error("err => " + err);
} else {
console.log(result);
callback();
}
})
});
apartment.update({}, {$set: {catName: default_}}, {multi:true}, (err, result) => {
if(err) {
console.error("err => " + err);
} else {
console.log(result);
callback();
}
}).exec();
}
}
module.exports = AddNewCategory;
{ ok: 0, n: 0, nModified: 0 }
db.apartments.update({}, {$set: {asd: "Tom"}}, {multi:true}), then the record is successfully updated
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
Answer the question
In order to leave comments, you need to log in
A day later, it turned out to do what was planned, only using a crutch of the form:
apartment.find({}, (err, apart) => {
if(err) return new Error('Error in find');
apart.forEach((v, k, apart) => {
apartment.findByIdAndUpdate(apart[k]._id, {$set: { dsfgfdg: 'fff'}}, {strict: false, new: true}, (err, ap) => {
if(err) return new Error('Error in save');
console.log(ap);
ap.save((err, ress) => {
//console.log(ress);
});
});
});
});
strict: false
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question