Thursday, 27 April 2017

how to get Mongoose save function WriteResult

I using node.js Mongoose to save (update) an object, and I would like to see the WriteResult that I think I should be able to get.

Reference: MongoDB insert operation returns WriteResult instead of inserted doc

I have a model:

var MyDataSchema = Schema({
  name: { type: String },
  mixdata: Schema.Types.Mixed
});

Where mixdata is actually an array of JSON values.

When the MyData object is saved, I would like to see the writeResult:

myData.save(function (err, writeResult) { 
  if (err) { 
    log.error(err); 
  } 
  log.debug('WriteResult: ', writeResultToString( writeResult)); 
});

writeResultToString = function( wr ){
  str = "WriteResult:";
  var keys = Object.keys( wr );
  for( var i in keys ){
    var key = keys[i];
    str += key + " (" + (typeof wr[key]) + ") ";
    if( typeof wr[key] != 'function' ){
        str += wr[key];
    }
  }
  return str;
}

But I am not seeing any of the "nMatched", "nUpserted", "nModified" properties in whatever was passed back in writeResult.

The writeResult object that I saw had:

isNew: (boolean) false
errors (undefined) undefined
_doc: (object) [object Object]
$_original_save (function)
save: (function)
_pres: (object)
_posts: (object)
$_original_validate (function)
validate: (function)
$_original_remove (function)
remove (function)
loadDate: (object) // today's date

This is what I think should be returned: https://docs.mongodb.com/manual/reference/method/db.collection.update/#writeresults-update

Do I need to call another function on the returned object, like in MongoDB weird writeResult behaviour

I don't see how I can use the writeResult object that was returned.



via J21042

No comments:

Post a Comment