Search code examples
mongodboperatorsloopbackjsstrongloop

How to use MongoDB operators in Strongloop


I want to use $inc for update an attribute of a Model (user), but I find two problems. I can't find if the parameter allowExtendedOperators:true, and I don't know if this is write correctly:

common/user.js

user.updateAttribute('coins',{ '$inc': {coins: -1} }, function(err,...);

common/user.json

"name": "user",
"base": "User",
"strict": true,
"idInjection": true,
"options": {
   "validateUpsert": true
}, 
...  
"settings": {
    "mongodb": {
      "allowExtendedOperators": true
    }
  },

I try this but nothing happen...

server/datasource.development.js

"MongoDB": {
    "host": "...",
    "port": "..."
    "database": "...",
    "name": "MongoDB",
    "connector": "mongodb",
    "allowExtendedOperators": true
  }

I was looking for on the documentation StrongLoop and the only example is to make a updateAll and says:

There are two ways to enable the allowExtendedOperators flag: in the model definition JSON file and as an option passed to the update method.

But nothing works to me..


Solution

  • Call the method as follows:

    user.updateAttributes({"$inc": {coins: -1}}, callback);
    

    although the callback will always return the old instance before decrementing.