$inc does not work


#1

Hello,

I don’t know why itemsNumber does not get decremented… Could someone help me ?

  Products.update(pId, {
    $inc: {itemsNumber: -1}, // decrement the items number
    $push: { // add a payment information
      payments: {
        date: new Date(),
        orderId: orderId,
        transactionId: transId,
        status: "sold"
      }
    }
  });

Thanks,
Mickael.


#2
Products.update({ _id: pId }, { $inc: { itemsNumber: -1 } }, {
   $push: { // add a payment information
      payments: {
        date: new Date(),
        orderId: orderId,
        transactionId: transId,
        status: "sold"
      }
    }
});

Untested but try to separate like this.


#3

That should work. What problem do you have? It does nothing or it throws an error?


#4

The problem is that the push is done, I have a new “payments” info, but the number “itemsNumber” does not change.


#5

It works with 2 differents lines

  OrbiterProducts.update(pId, {$inc: {itemsNumber: -1}}); // decrement the items number
  OrbiterProducts.update(pId, {
    $push: { // add a payment information
      payments: {
        date: new Date(),
        orderId: orderId,
        transactionId: transId,
        status: "sold"
      }
    }
  });

#6

That’s bizarre, it should work the original way! Can you submit an issue?


#7

should be

Products.update({ _id: pId }, { $inc: { itemsNumber: -1 },  // remove extra object here
   $push: { // add a payment information
      payments: {
        date: new Date(),
        orderId: orderId,
        transactionId: transId,
        status: "sold"
      }
    }
});