Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created May 6, 2010 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnunemaker/392450 to your computer and use it in GitHub Desktop.
Save jnunemaker/392450 to your computer and use it in GitHub Desktop.
MongoDB compound unique index
require 'rubygems'
require 'mongo'
uid, aid = BSON::ObjectID.new, BSON::ObjectID.new
db = Mongo::Connection.new.db('testing')
db.drop_collection('ratings')
db.create_collection('ratings')
ratings = db['ratings']
ratings.create_index([[:user_id, Mongo::ASCENDING], [:article_id, Mongo::ASCENDING]], :unique => true)
ratings.save({:user_id => uid, :article_id => aid}, :safe => true)
ratings.save({:user_id => uid, :article_id => aid}, :safe => true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment