I am trying this at the mongodb console:
db.foobar.update(    { name: "Foobar" },   {      $set : { foo: { bar: 'bar' },      $inc: { 'foo.count': 1 }    }  }, true) It returns with "ok", but db.foobar.find(), returns an empty record set. I'm trying to upsert a document, so it looks like the:
name: Foobar foo: {   bar: 'bar'   count: 1 } If the doc doesn't exist then create one with a count of 1. Otherwise, just increase the count. Why isn't above working?
In MongoDB, the $inc operator is used to increment the value of a field by a specified amount. The $inc operator adds as a new field when the specified field does not exist, and sets the field to the specified amount. The $inc accepts positive and negative value as an incremental amount.
It seems to me that your code is actually trying to set the $inc field of the document rather than using the $inc modifier on the foo.count field. This might be what you want:
db.foobar.update(     { name: "Foobar" },      {         $set: { 'foo.bar': 'bar' },          $inc: { 'foo.count': 1 }      }, true) Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With