1

I am developing API using loopback + mongoDB. In that i have to update an array in a document in mongoDB:

My model:

{
  "userId": "546fgdfgd445",
  "rrrr": "7",
  "bbbbb": [
    {}
  ],
  "id": "57a437789521b58b12124016"
}

I need to update the array with new element with the data sent form client:

app.models.vvvvv.update({ userId: '546fgdfgd445' },{ $push: { "transactions": transactionData }}, function(err, res){
        if(err){
            console.log(err);
        } else {
            console.log(res);
        }
    });

But i am receiving error:

{ name: 'MongoError',
  message: 'The dollar ($) prefixed field \'$push\' in \'$push\' is not valid for storage.',
  driver: true,
  index: 0,
  code: 52,
  errmsg: 'The dollar ($) prefixed field \'$push\' in \'$push\' is not valid for storage.' }

I am using mongoDb version 3.2.3.

Please share your ideas. Thanks in advance.

3
  • What is transactionData ?Seems there is something wrong with that. Commented Aug 5, 2016 at 7:15
  • @Shrabanee No..its perfect.. Its an object,,, I am trying to push object ({}) into athe array..\ Commented Aug 5, 2016 at 9:03
  • Can you try doing this in mongo shell directly? I tried the way you have written and it is working fine. Commented Aug 5, 2016 at 9:07

1 Answer 1

1

in model.json you need to enable extended operations like this :

"options": {    
    "mongodb": {
      "collection": "model_collection",
      "allowExtendedOperators": true
    }
  },

or

    app.models.vvvvv.update({ userId: '546fgdfgd445' },
{ $push: { "transactions": transactionData }}, { allowExtendedOperators: true }, function(err, res){
            if(err){
                console.log(err);
            } else {
                console.log(res);
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.