M
M
Maxim Sokhryakov2019-01-26 21:58:26
MongoDB
Maxim Sokhryakov, 2019-01-26 21:58:26

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;

My goal is to create a new field in the given schema. I get the name and default value via a post request.
What I tried to do:
module code
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;

Went through all the ways that I managed to google. None of the methods worked. For all output result:
{ ok: 0, n: 0, nModified: 0 }

I tried in the condition of the search for records instead of {} to set both the _id of one of the records, and one of the fields that is guaranteed to be in the database. Everything returns that no records were found, which means nothing has been updated.
ps If you write through the console
db.apartments.update({}, {$set: {asd: "Tom"}}, {multi:true})
, then the record is successfully updated
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })

ps
one of the entries
{
"_id" : ObjectId("5c4c4a62d1160f1eeece7aa8"),
"transport" : [
"24",
"35"
],
"price" : 12,
"square" : 23,
"ecoSituation" : "5",
"typeOfHouse" : "house",
"numberOfRooms" : 12,
"updated" : ISODate("2019-01-26T11:54:10.927Z"),
"upgraded" : ISODate("2019-01-26T11:54:10.927Z"),
"__v" : 0,
"asd" : "Tom"
}
{
"_id" : ObjectId("5c4c4b3cd1160f1eeece7aa9"),
"transport" : [
"24"
],

pss having .exec() didn't give any result. Both with and without a callback, exec() did nothing either.
psss I feel like the answer is somewhere on the surface, but I can't figure out where exactly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Sokhryakov, 2019-01-27
@maximsohryakov

A day later, it turned out to do what was planned, only using a crutch of the form:

spoiler
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);
          });
        });
      });
    });

The whole point was in the property
strict: false

What the documentation says:
I don't know if to keep or delete. It may be useful to someone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question