Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb's $inc, incrementing a field that does not exists (upsert case)

Tags:

mongodb

upsert

What happens when you increment a field that does not exist (upsert case) ?...

db.collection.findAndModify({query:{id:"id_in_param"},
                             update:{$inc:{score:1}},
                             upsert:true
                           })
like image 585
L.G Avatar asked Jul 31 '18 09:07

L.G


1 Answers

The document said:

If the field does not exist, $inc creates the field and sets the field to the specified value.

In your case, new field score: 1 is added

like image 132
Mạnh Quyết Nguyễn Avatar answered Sep 19 '22 03:09

Mạnh Quyết Nguyễn