Giter Club home page Giter Club logo

index-model's Introduction

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

index-model's People

Contributors

aherlihy avatar durran avatar greenkeeperio-bot avatar imlucas avatar kangas avatar pzrq avatar rueckstiess avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.