mongodb-index-model
MongoDB index model.
Last updated 6 months ago by imlucas .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mongodb-index-model 
SYNC missed versions from official npm registry.

mongodb-index-model

MongoDB Index toolkit for humans.

Index Mental Model Diagram

Example

var IndexModel = require('mongodb-index-model');

// e.g. from collection.getIndexes()
var indexDefs = [
  {
    'v': 1,
    'key': {
      '_id': 1
    },
    'name': '_id_',
    'ns': 'mongodb.fanclub'
  },
  {
    'v': 1,
    'key': {
      'last_login': -1
    },
    'name': 'last_login_-1',
    'ns': 'mongodb.fanclub'
  }
];

var indexes = new IndexModel(indexDefs, {parse: true});

// get index by `<namespace>.<name>`
indexes.get('mongodb.fanclub.last_login_-1').compound  // returns `false`

// get index by `<name>` (use `name` ampersand index)
indexes.get('_id_', 'name').unique // returns `true`

Installation

npm install --save mongodb-index-model

Testing

npm test

Index Mental Model

General

A collection can have many indexes. An index can have one or more field/value pairs. We use JSON notation to represent and index definition as a document of field/value pairs, e.g. { last_name: 1, location: "2dsphere" }. The index field corresponds to a field in the documents stored in the collection. The value defines the type, and possibly subtype or sort order of the index on this field. An index can have additional properties.

Index Field Types

Index field types are the following:

  • regular (note: the MongoDB Documentation does not mention this type of index explicitly, but rather groups indexes into "single", "compound" and "multi-key". This distinction is not necessary here and would complicate the mental model). The values for regular index fields indicates sort order: 1 for ascending, or -1 for descending sort order.
  • text, with the only possible value text
  • hashed, with the only possible value hashed
  • geospatial, where the value specifies the sub-type of geo index, which can be 2d for a flat, cartesian geometry, 2dsphere for an index assuming a spherical geometry, or geoHaystack for a (rarely used) haystack index.

In summary, given the different types of index and their allowed values, a field value in an index defintion can be one of: 1, -1, "text", "hashed", "2d", "2dsphere", "geoHaystack".

Index Properties

An index can have one or more properties. These are independent of the fields and values and defined per index. Index properties are:

  • TTL "time to live", automatically deletes documents after some time.
  • unique enforces that no duplicate values exist for this field
  • sparse means documents without the specified field(s) are not indexed, default is that every document is indexed
  • partial takes a filter expression and only indexes documents matching that expression. similar to sparse but more flexible.

Index properties are specified as a separate second parameter to the createIndex() method. There are limitations which index types and properties can be used together, not every combination is possible.

Index Mental Model Diagram

License

Apache 2.0

Current Tags

  • 2.6.1                                ...           latest (6 months ago)

31 Versions

  • 2.6.1                                ...           6 months ago
  • 2.6.0                                ...           8 months ago
  • 2.5.0                                ...           a year ago
  • 2.4.0                                ...           2 years ago
  • 2.3.0                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.6.5                                ...           3 years ago
  • 0.6.4                                ...           4 years ago
  • 0.6.3                                ...           4 years ago
  • 0.6.2                                ...           4 years ago
  • 0.6.1                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.5.5                                ...           4 years ago
  • 0.5.4                                ...           4 years ago
  • 0.5.3                                ...           4 years ago
  • 0.5.2                                ...           5 years ago
  • 0.5.1                                ...           5 years ago
  • 0.5.0                                ...           5 years ago
  • 0.4.2                                ...           5 years ago
  • 0.4.1                                ...           5 years ago
  • 0.4.0                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.3                                ...           5 years ago
  • 0.1.2                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago

Copyright 2014 - 2016 © taobao.org |