Roland Tanglao
About

upsert using ruby 2.3, mongodb 3 and driver 2

Jan 3, 2016

Update and insert all in one go! (I’m sure this worked with ruby mongo driver version 1. I just didn’t realize it until I read this stackoverflow question on upsert). Here’s the relevant code snippet (“id” is the instagram string unique id not to be confused with mongo’s unique id which is “_id”):

photosColl.find({ 'id' => id }).update_one(photo,:upsert => true )    

Leave a comment on github

Roland Tanglao

  • Roland Tanglao
  • roland@rolandtanglao.com
  • rtanglao
  • rtanglao

flickr.com/roland, twitter.com/rtanglao #firefox #thunderbird