Skip to content

Instantly share code, notes, and snippets.

@durran
Created December 29, 2011 12:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save durran/1533903 to your computer and use it in GitHub Desktop.
Save durran/1533903 to your computer and use it in GitHub Desktop.
Integer sequence ids in Mongo
Mongoid.database.add_stored_function "sequence", <<-__
function(name) {
var ret = db.counters.findAndModify({ query: { _id: name}, update: { $inc : { next: 1}}, "new" :true, upsert: true});
return ret.next;
}
__
class Sequence
include Mongoid::Fields::Serializable
def deserialize(object)
object.to_i
end
def serialize(object)
Mongoid.master.eval("sequence('#{object}')").to_i
end
end
class A
include Mongoid::Document
field :_id, :type => Sequence
end
A.create(:_id => "as")
@ahoward
Copy link

ahoward commented Dec 29, 2011

this is really excellent. a question though: is 'add_stored_function' idempotent? or is it ok to always add it while other processes are using it?

@durran
Copy link
Author

durran commented Dec 29, 2011

That I am not sure, I'll have to experiment a bit.

@ahoward
Copy link

ahoward commented Dec 29, 2011

check out my fork: https://gist.github.com/1535180

@ahoward
Copy link

ahoward commented Dec 29, 2011

one thing i'm am concerned about is associations: my guess is that a lot of the mongoid code is going to connect objects based on the assumption that ids were baked in at creation time, rather than at save time. my fork ensures a nil _id to shit would blow up fast if it relied on having an _id before saving... any thoughts on that? think associations will bear late binding the id?

@ahoward
Copy link

ahoward commented Jan 7, 2012

i played around with this and i think it'd be pretty hard to make mongoid wait until save to assign _id. OTOH the mongo ruby driver supports this easily with PK factories - but associations really make this tricky. in general a refactor to support making ids until right before saving would be nice but i am not sure the trade-offs would be worth it. it is really nice having ids in memory available before save...

@durran
Copy link
Author

durran commented Jan 7, 2012

Yeah we're talking another rewrite of the association code in order to make that happen, and it would definitely be slower since Mongoid would have to reiterate through all the new documents before the save of any single one to set all the ids and foreign keys instead of just setting the id when instantiated and setting the foreign key when a document is added to an association. I don't want us to start getting to AR speeds. :)

@ahoward
Copy link

ahoward commented Jan 7, 2012

yes i can understand that. somewhat humorously i have actually had AR projects configured so that it assignes ids up front to make circular associations simpler ;-)

you know, top level ids and intra-document ids are not really the same thing.... i've considered before both having nice ids on top level docs and object_ids for intra-document relations...

in the end it is indeed very difficult to beat the overall tradoffs object_ids provide. lately i've just been slugging the top level namespace in my urls, for instance

/dojo4/posts/comments/$object_id

and that addresses the 80% rule pretty well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment