Tutorials on upserts in mongodb

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

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 …

upsert

upsert

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.