Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am mdirolf on github.
  • I am mdirolf (https://keybase.io/mdirolf) on keybase.
  • I have a public key whose fingerprint is C16B E0A1 472C AE1A 0044 D0C9 ABB0 0EC7 1F5B 997F

To claim this, I am signing this object:

@mdirolf
mdirolf / gist:468712
Created July 8, 2010 21:59
MongoDB insert if not present
> db.test.find()
> db.test.update({foo: 1, bar: 2}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
> db.test.update({foo: 1, bar: 2}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
> db.test.update({foo: 1}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
db.test.drop();
db.test.save({authors: ["mike"]});
db.test.save({authors: ["mike", "eliot"]});
db.test.save({authors: ["eliot", "dwight", "steve", "mike"]});
db.test.save({authors: ["steve", "mike"]});
var res = db.test.mapReduce(
function() {
var that=this;
that.authors.forEach(function(a){
require 'rubygems'
require 'mongo'
require 'pp'
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
puts "Connecting to #{host}:#{port}"
db = XGen::Mongo::Driver::Mongo.new(host, port).db('jon')
coll = db.collection('test')