(Quick Reference)

index

Purpose

Defines properties that should be indexed by MongoDB

Examples

class Book {
	String title
	Date releaseDate
	Author author
	String ISBN

static indexes = { idx_isbn unique:true, dropDups:true, fields:[desc('ISBN')] idx_author sparse:true, fields:[asc('author')] idx_book fields:['author', desc('title')] // asc is the default direction } }

Description

Syntax is the following:

<index name> <index properties>

Where <index properties> may be:

  • unique: guarantees that no two documents have the same values for given fields. Defaults to false
  • dropDups: if you create unique indexes on existing data, setting dropDups to true tells MongoDB to drop documents with same unique values. Defaults to false
  • sparse: creates a sparse index. Defaults to false
  • fields: a list of fields that should be indexed. You can set the index direction by enclosing the property names by the sorting keywords asc or desc.

See also

.