Tutorials on upserts in mongodb
An upsert is like an update, but will do an insert if record was not found. Continuing from our last example, we made changes to the following …
Since our collection does have “state” of “New State”, the query found a document and replaced it with the new “doc”. But if it did not, it would have inserted the new “doc”. In either case, it would have return 1 as num_updated.