loopback-connector-mongodb icon indicating copy to clipboard operation
loopback-connector-mongodb copied to clipboard

updateAttributes() returns either old or updated instance

Open voitau opened this issue 11 years ago • 5 comments

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

voitau avatar Jul 03 '15 17:07 voitau

+1 I am seeing this with $inc operation too user.updateAttributes({"$inc": {balance: 100}}, callback);

The callback always returns the old instance before incrementing

satyadeepk avatar Aug 16 '15 20:08 satyadeepk

It is probably related to the "new" option on mongoDBs findAndModify function. You can see the documentation here.

zanemcca avatar Aug 28 '15 16:08 zanemcca

We should make it explicit via the options arg. But not all DBs support it.

raymondfeng avatar Aug 28 '15 22:08 raymondfeng

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.

ssh24 avatar Jun 20 '17 13:06 ssh24

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.

chi-tran avatar Mar 21 '19 08:03 chi-tran