Updating a subfield in a mongoDB document with findOne and saving

I am trying to update a specific subfield in a mongoDB document and decided to first find the object in question and then save the updated one. For some reason, the save option seems to ignore my changes.

I have one object in my collection and it matches the following scheme:

var tschema= mongoose.Schema({
a: Object
})

var t = db.model('tongoose',tschema);
t.findOne({},function(err,obj){
  console.log(obj.a); //yields ['banana',3]     
  obj.a[1]=1; //to make ['banana',1]
  console.log(obj); //yields ['banana',1]

  obj.save(function(err,real){
    console.log(real); //yields ['banana',1]
  });
});

But when I go back to mongoDB and look at the saved object, it never shows any changes. Can you tell me what I'm doing wrong?

Great importance.

+4
source share
1 answer

a , Mixed Mongoose, , markModified save .

obj.markModified('a');
obj.save(function(err,real){ ...

. Mixed .

+7

All Articles