Method: Mongo::Collection#create_index
- Defined in:
- lib/jmongo/collection.rb
#create_index(spec, opts = {}) ⇒ String
Create a new index.
Examples:
Creating a compound index:
Creating a compound index:
@posts.create_index([['subject', Mongo::ASCENDING], ['created_at', Mongo::DESCENDING]])
Creating a geospatial index:
Creating a geospatial index:
@restaurants.create_index([['location', Mongo::GEO2D]])
# Note that this will work only if 'location' represents x,y coordinates:
{'location': [0, 50]}
{'location': {'x' => 0, 'y' => 50}}
{'location': {'latitude' => 0, 'longitude' => 50}}
A geospatial index with alternate longitude and latitude:
A geospatial index with alternate longitude and latitude:
@restaurants.create_index([['location', Mongo::GEO2D]], :min => 500, :max => 500)
Parameters:
-
spec
(String, Array)
—
should be either a single field name or an array of
- field name, direction
-
pairs. Directions should be specified
as Mongo::ASCENDING, Mongo::DESCENDING, or Mongo::GEO2D.
Note that geospatial indexing only works with versions of MongoDB >= 1.3.3+. Keep in mind, too, that in order to geo-index a given field, that field must reference either an array or a sub-object where the first two values represent x- and y-coordinates. Examples can be seen below.
Also note that it is permissible to create compound indexes that include a geospatial index as long as the geospatial index comes first.
-
unique
(Boolean)
—
if true, this index will enforce a uniqueness constraint. DEPRECATED. Future versions of this driver will specify the uniqueness constraint using a hash param.
-
opts
(Hash)
(defaults to: {})
—
a customizable set of options
Options Hash (opts):
-
:unique
(Boolean)
— default:
false
—
if true, this index will enforce a uniqueness constraint.
-
:background
(Boolean)
— default:
false
—
indicate that the index should be built in the background. This feature is only available in MongoDB >= 1.3.2.
-
:dropDups
(Boolean)
—
If creating a unique index on a collection with pre-existing records, this option will keep the first document the database indexes and drop all subsequent with duplicate values.
-
:min
(Integer)
—
specify the minimum longitude and latitude for a geo index.
-
:max
(Integer)
—
specify the maximum longitude and latitude for a geo index.
Returns:
-
(String)
—
the name of the index created.
372 373 374 |
# File 'lib/jmongo/collection.rb', line 372 def create_index(spec, opts={}) _create_index(spec, opts) end |