-
-
Save durran/1533903 to your computer and use it in GitHub Desktop.
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") |
That I am not sure, I'll have to experiment a bit.
check out my fork: https://gist.github.com/1535180
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?
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...
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. :)
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.
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?