updateAttributes() returns either old or updated instance
Based on the documentation, updateAttributes() is supposed to return updated instance. However, behavior is not completely consistent.
Updated instance is returned here:
updateAttributes({description: 'v1', version: 1}, function(err, updated) {...})
This will return old instance:
updateAttributes({'$set': {description: 'v1', version: 1}}, function(err, old) {...})
Complete example: https://github.com/voitau/loopback-examples/blob/master/mongo-connector/test/petition.test.js#L44-L51
+1
I am seeing this with $inc operation too
user.updateAttributes({"$inc": {balance: 100}}, callback);
The callback always returns the old instance before incrementing
It is probably related to the "new" option on mongoDBs findAndModify function. You can see the documentation here.
We should make it explicit via the options arg. But not all DBs support it.
This is not a bug anymore. I can reproduce updateAttributes returning only the updated instance.
Here is a sample code snippet:
model-definition.json
{
"name": "Employee",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
boot-script.js
'use strict';
var util = require('util');
var _ = require('lodash');
module.exports = function(app) {
var db = app.datasources.mongoDs;
var Employee = app.models.Employee;
db.automigrate(function(err) {
if (err) throw err;
console.log('\nAutomigrate completed');
Employee.create([{
name: 'Foo',
age: 1
}, {
name: 'Bar',
age: 2
}], function(err, result) {
if (err) throw err;
console.log('\nCreated instance: ' + util.inspect(result));
Employee.find(function(err, result) {
if (err) throw err;
console.log('\nFound instance before update: ' + util.inspect(result));
var employeeInstance = new Employee(result[0]);
var newData = _.cloneDeep(result[0]);
newData.name = 'Foo-updated';
console.log('\nData to be updated: %j', newData);
employeeInstance.updateAttributes(newData, {validate: false},function(err, result) {
if (err) throw err;
console.log('\nUpdateAttribute on instance 1: %j', result);
Employee.find(function(err, result) {
if (err) throw err;
console.log('\nFound instance after update: ' + util.inspect(result));
});
});
});
});
});
};
output
sakibs-mac:mongodb-157 ssh$ node .
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer
Automigrate completed
Created instance: [ { name: 'Foo', age: 1, id: 5948d308ba4fe817a6c4704f },
{ name: 'Bar', age: 2, id: 5948d308ba4fe817a6c47050 } ]
Found instance before update: [ { name: 'Foo', age: 1, id: 5948d308ba4fe817a6c4704f },
{ name: 'Bar', age: 2, id: 5948d308ba4fe817a6c47050 } ]
Data to be updated: {"name":"Foo-updated","age":1,"id":"5948d308ba4fe817a6c4704f"}
UpdateAttribute on instance 1: {"name":"Foo-updated","age":1,"id":"5948d308ba4fe817a6c4704f"}
Found instance after update: [ { name: 'Foo-updated', age: 1, id: 5948d308ba4fe817a6c4704f },
{ name: 'Bar', age: 2, id: 5948d308ba4fe817a6c47050 } ]
Closing this issue as it does not exist anymore. Feel free to reopen if needed.
This should not be closed. The $inc returns the old data set. The example only updates an attribute and that works. the $inc does not.