Method: Mongo::Index::View#create_one

Defined in:
lib/mongo/index/view.rb

#create_one(keys, options = {}) ⇒ Result

Note:

Note that the options listed may be subset of those available.

Creates an index on the collection.

See the MongoDB documentation for a full list of supported options by server version.

Examples:

Create a unique index on the collection.

view.create_one({ name: 1 }, { unique: true })

Parameters:

  • keys (Hash)

    A hash of field name/direction pairs.

  • options (Hash) (defaults to: {})

    Options for this index.

Options Hash (options):

  • :unique (true, false) — default: false

    If true, this index will enforce a uniqueness constraint on that field.

  • :background (true, false) — default: false

    If true, the index will be built in the background (only available for server versions >= 1.3.2 )

  • :drop_dups (true, false) — default: false

    If creating a unique index on this collection, this option will keep the first document the database indexes and drop all subsequent documents with duplicate values on this field.

  • :bucket_size (Integer) — default: nil

    For use with geoHaystack indexes. Number of documents to group together within a certain proximity to a given longitude and latitude.

  • :max (Integer) — default: nil

    Specify the max latitude and longitude for a geo index.

  • :min (Integer) — default: nil

    Specify the min latitude and longitude for a geo index.

  • :partial_filter_expression (Hash)

    Specify a filter for a partial index.

  • :hidden (Boolean)

    When :hidden is true, this index will exist on the collection but not be used by the query planner when executing operations.

  • :commit_quorum (String | Integer)

    Specify how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. Potential values are:

    • an integer from 0 to the number of members of the replica set

    • “majority” indicating that a majority of data bearing nodes must vote

    • “votingMembers” which means that all voting data bearing nodes must vote

  • :session (Session)

    The session to use for the operation.

  • :comment (Object)

    A user-provided comment to attach to this command.

Returns:

  • (Result)

    The response.

Since:

  • 2.0.0



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mongo/index/view.rb', line 157

def create_one(keys, options = {})
  options = options.dup

  create_options = {}
  if session = @options[:session]
    create_options[:session] = session
  end
  %i(commit_quorum session comment).each do |key|
    if value = options.delete(key)
      create_options[key] = value
    end
  end
  create_many({ key: keys }.merge(options), create_options)
end