summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/docs/indexes.md
blob: f2943188640159ecb56e47fdd84107de27167d6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Indexes
=======

Indexes are defined through `ensureIndex` every time a model is compiled for a
certain connection / database. This means that indexes will only be ensured
once during the lifetime of your app.

## Definition

Regular indexes:

    var User = new Schema({
        name: { type: String, index: true }
    })

[Sparse](http://www.mongodb.org/display/DOCS/Indexes#Indexes-SparseIndexes) indexes:

    var User = new Schema({
        name: { type: String, sparse: true }
    })

Unique indexes:

    var User = new Schema({
        name: { type: String, unique: true }
    })

    // or

    var User = new Schema({
        name: { type: String, index: { unique: true } }
    })

Unique sparse indexes:

    var User = new Schema({
        name: { type: String, unique: true, sparse: true }
    })

Compound indexes are defined on the `Schema` itself.

    User.index({ first: 1, last: -1 }, { unique: true })