Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save victorbstan/743008 to your computer and use it in GitHub Desktop.

Select an option

Save victorbstan/743008 to your computer and use it in GitHub Desktop.
A couple of short commands to add unique constraints on sequential index fields in a mongoDB document
// Mongo shell
// Let's say you need to have a sequential id; here, it is '_i'
// the other entry is a reference to another 'post' containing its natural MongoDB ObjectId
db.your_collection_name.insert({"_i":new NumberLong(1), "post_id":"4cf68ff393eae6291e00003a"});
// let's make sure we add an index that is defined as unique
db.your_collection_name.ensureIndex({"_i":1}, {unique:true});
// TODO: still need to provide the methodology used to auto-increment the unique field '_i'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment