Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update $inc with Mongoose other behavior then MongoDB update

I try to update a document with mongoose and it fails. The query I can successful execute directly in Mongo is like:

db.orders.update(
    { 
        orderId: 1014428,
        'delivery.items.id': '5585d77c714a90fe0fc2fcb4' 
    },
    {
        $inc: {
            "delivery.items.$.quantity" : 1
        }
    }
)

When I try to run the following update command with mongoose:

this.update(
        {
            orderId: this.orderId ,
            "delivery.items.id": product.id
        },
        {
            $inc: {
                "delivery.items.$.quantity" : 1
            }
        }, function (err, raw) {
            if (err) {
                console.log(err);
            }

            console.log('The raw response from Mongo was ', raw);
        }
    );

I see the following error:

{ [MongoError: cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})]
  name: 'MongoError',
  message: 'cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})',
  index: 0,
  code: 16837,
  errmsg: 'cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})' }
The raw response from Mongo was  { ok: 0, n: 0, nModified: 0 }

I tried so many things. Any advice on this?

As requested the schema:

var Order = new Schema({
    orderId: Number,
    orderDate: String,
    customerName: String,
    state: Number,
    delivery: {
         items: {type: Array, default: []},
         state: { type: Number, default: 0 }
    }
});
like image 697
Tobias Redmann Avatar asked Oct 20 '22 08:10

Tobias Redmann


1 Answers

TL;DR: use your model Order instead of an instance this when doing more advanced queries:

Orders.update(
    {
        orderId: this.orderId ,
        "delivery.items.id": product.id
    },
    {
        $inc: {
            "delivery.items.$.quantity" : 1
        }
    }, function (err, raw) {
        if (err) {
            console.log(err);
        }

        console.log('The raw response from Mongo was ', raw);
    }
);

Explanation:

Mapping differences between Model.update() and Document.update().

The using the model, then Model.update() will be used and

Model.update(conditions, doc, options, callback)

will be mapped to:

db.collection.update(query = conditions, update = doc, options)

When using an instance instead your calling Document.update() and

Document.update(doc, options, callback)

will be mapped to the following:

db.collection.update(query = {_id: _id}, update = doc, options)
like image 65
Kevin Sandow Avatar answered Oct 21 '22 23:10

Kevin Sandow