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?
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:
Hope this helps.