Mongoose Schema.update doesn't update boolean

mongoose update
mongoose findoneandupdate
mongoose update not working
mongoose schema methods
mongoose updatemany
mongoose update multiple fields
mongoose upsert
mongoose change tracking

I have tried updating other fields and it works just fine.

The command I am using in my API:

  User.update({ email: targetUser.email }, { $set: { isAdmin: true }, $push: { 'log.updated': new Date() } }, function (err, user) {
  if (err) {
    responseObject.err = err;
    responseObject.data = null;
    responseObject.code = 422;

    return res.json(responseObject);
  }


  return res.json(responseObject);
});

To clarify, when I try to run this, the API returns a code 200, meaning everything worked fine, but when I check the database the isAdmin value wasn't changed.

Any suggestions would be helpful, running out of ideas here!

User Schema as requested:

var UserSchema = new Schema({
  name: { type: String, default: "", index: 'text' },
  email: { type: String, lowercase: true },
  role: { type: String, default: "" },
  meta: {
    skills: { type: Array, default: [], index: 'text' },
    about: { type: String, default: "", index: 'text' },
    education: { type: Array, default: [], index: 'text' },
    location: {
      address: {
        a: { type: String, default: "" },
        p: { type: String, default: "" },
        c: { type: String, default: "" }
      },
      geo: {
        lat: { type: Number, default: 0 },
        lng: { type: Number, default: 0 }
      }
    }
  },

  compMeta:
  {
    departments: { type: Array, default: [], index: 'text' },
    employees:
    [
      {
        emId: Number,
        empName: String,
        empDep: String  // Dunno if i should use Dep name or Dep ID gonna look in to that later
      }
    ],
  }
  ,
  settings: {
    search: {
      distance: {
        n: { type: Number, default: 100 },
        t: { type: String, default: "km" }
      }
    }
  },
  created: {
    type: Date,
    default: Date.now
  },
  //Rating is an array of objects that consist of rateing 0-100 , job database id , comments from the Company
  rating:
  [
    {
      rate: Number,
      jobId: Number,
      jobComments: String
    }
  ],
  /*rating:
  {
    userTotalRating: {type: Number, default: 0},
    ratingCounter : {type: Number, default: 0}
  }*/


  sensitive: {
    cpr_cvr: String,
  },
  stripe: { type: String },
  facebook: {},
  linkedin: {},
  log: {
    updated: { type: Array, default: [] }
  },
  hashedPassword: String,
  provider: { type: String, default: 'local' },
  salt: String
});

UPDATE:

Mongodb version: 3.0.7

Turns out I just forgot to add the isAdmin field to my User Schema! Also, my call to the update was wrong, I changed it to this:

User.update({ email: targetUser.email }, { $set: { isAdmin: true }}, { $push: { 'log.updated': new Date() } }, function (err, user) {
  if (err) {
    responseObject.err = err;
    responseObject.data = null;
    responseObject.code = 422;

    return res.json(responseObject);
  }


  return res.json(responseObject);
});

Thanks to everyone that put an effort to help me! :)

Model.update, Model.update. Updates all documents matching conditions using the update clause. All update values are casted to their appropriate types before being sent. This will happen whenever a key's value is a POJO that doesn't have a type property. In these cases, Mongoose only creates actual schema paths for leaves in the tree. (like meta.votes and meta.favs above), and the branches do not have actual paths. A side-effect of this is that meta above cannot have its own validation.

Try updating two fields with $set

User.update({ email: targetUser.email }, { $set: { isAdmin: true, 'log.updated': new Date() } }, function (err, user) {
 if (err) {
responseObject.err = err;
responseObject.data = null;
responseObject.code = 422;

return res.json(responseObject);
 }
 return res.json(responseObject);
});

Hope it's works.

Mongoose v5.9.27: Schemas, Learn about the different ways to update a document in Mongoose. CharacterModel = mongoose.model('Character', schema); const doc = await updateOne() doesn't require you to load the document into memory, which� Hi, I've a problem with the newest version of Mongoose. I'm creating an API with Express and Mongoose 4.0.1, and I'm not sure if I'm doing anything wrong, but the fact is whenever I try to use the new pre update middleware the same way I use pre save middleware, this object does not return the object being updated, instead it returns the Query

I encountered a similar problem. The solution was to add the callback.

This doesn't work:

Ride.updateOne({driver:req.body.id},{$set:{isBusy:true}});

This works:

Ride.updateOne({driver:req.body.id},{$set:{isBusy:true}},(e,s)=>{});

Model, MongoDB is a document database with the scalability and flexibility that you want 9. update() :- update() is used for updating a record based on condition. it can If we won't provide any condition it will remove all record. Seems like mongoose doesn't even need all the "update" style functions at all then since they are so dangerous and poorly documented. 👍 2 👎 1 This comment has been minimized.

Updating Documents in Mongoose, When I try to update a deep nested field, the query is well sent to MongoDB but when I do a find on this doc, the field is not updated. Example var DtSchema� In an earlier article I explained that although MongoDB stores documents in a "schema-less" collection you still have to think about how you store your data if you want to get the best out of the database.

Mongoose(mongoDB) functions for CRUD Application, Schema mongoose.connect('mongodb://localhost/test') var Thing = new Schema( { title : String, The pre is triggered by the initial save, but not the update. section makes a reference to issues/964 which explains why this does not work. Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

Schema.type.Mixed and deep modification does not update , If I call update on the model: Contact.update({phone:request.phone}, contact, { upsert: true}, function(err{}) Data Models¶. Data in MongoDB has a flexible schema. Collections do not enforce document structure by default. This flexibility gives you data-modeling choices to match your application and its performance requirements.

Comments
  • What's the question/problem? It's not clear
  • I've added a small clarification, hope it helps :)
  • Can I see the schema model too?
  • @Michelem, I've updated the question with the schema!
  • I can't see isAdmin field
  • Exactly the same here (:
  • Hey Mariya, unfortunately I get the same result.
  • Tried that too, no change.
  • One last try.. updated the code. One more thing if it success then you will get true as response
  • Same thing, returns a 200 code and then nothing was changed in the DB.