Create and drop indexes for a single collection in Mongoid 3.0

1,564 views
Skip to first unread message

Andreas Saebjoernsen

unread,
Jun 9, 2012, 1:45:06 PM6/9/12
to mon...@googlegroups.com
Some gems we depend upon drop and create indexes during testing. In Mongoid 2.4 this can be done with:
  - Dropping indexes if there are any:  Book.collection.drop_indexes
  - Creating indexes if there are any:  Book.collection.create_indexes

I can perform the same functionality by accessing the index through the session, but is there a more concise way of creating and dropping indexes in Mongoid 3.0?

Best,
Andy

Durran Jordan

unread,
Jun 10, 2012, 5:57:29 AM6/10/12
to mon...@googlegroups.com
For both calls:

Book.remove_indexes
Book.create_indexes

If you want to drop all indexes on a collection, even ones that aren't defined in Mongoid:

Book.collection.indexes.drop


2012/6/9 Andreas Saebjoernsen <and...@digitalplaywright.com>

Andreas Saebjoernsen

unread,
Jun 10, 2012, 1:19:43 PM6/10/12
to mon...@googlegroups.com
Thank you. The gem in question, mongoid-slug, also use a composite key made from two fields. I can not find any documentation on how to create composite keys in Mongoid 3.0. Please let me know if this is still supported. 

This is a simple example on how composite keys are used in mongoid-slug:

class Animal
  include Mongoid::Document

  field :name
  field :nickname

  key :name, :nickname

end

Which I could change to the new syntax:

  field :_id, type: String, default: ->{ default_field }

I was not aware of the 'key :field_1, :field_2' syntax before looking at this gem and
can not see anything about it in the documentation. Does the
'key :field_1, :field_2' syntax have an equivalent and supported analogue in 
Mongoid 3.0? 
 

Best,
Andy
Reply all
Reply to author
Forward
0 new messages