A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mongodb.github.io/node-mongodb-native/markdown-docs/indexes.html below:

Indexes — MongoDB Node.JS Driver 1.4.9 documentation

Indexes

Indexes are needed to make queries faster. For example if you need to find records by a field named username and the field has a related index set, then the query will be a lot faster compared to if the index was not present.

See MongoDB documentation for details.

Create indexes with createIndex()

createIndex adds a new index to a collection. For checking if the index was already set, use ensureIndex instead.

collection.createIndex(index[, options], callback)

or

db.createIndex(collectionname, index[, options], callback)

where

Ensure indexes with ensureIndex()

Same as createIndex with the difference that the index is checked for existence before adding to avoid duplicate indexes.

Index field

Index field can be a simple string like "username" to index certain field (in this case, a field named as username).

collection.ensureIndex("username",callback)

It is possible to index fields inside nested objects, for example "user.firstname" to index field named firstname inside a document named user.

collection.ensureIndex("user.firstname",callback)

It is also possible to create mixed indexes to include several fields at once.

collection.ensureIndex({firstname:1, lastname:1}, callback)

or with tuples

collection.ensureIndex([["firstname", 1], ["lastname", 1]], callback)

The number value indicates direction - if it’s 1, then it is an ascending value, if it’s -1 then it’s descending. For example if you have documents with a field date and you want to sort these records in descending order then you might want to add corresponding index

collection.ensureIndex({date:-1}, callback)
Remove indexes with dropIndex()

All indexes can be dropped at once with dropIndexes

collection.dropIndexes(callback)

callback gets two parameters - an error object (if an error occured) and a boolean value true if operation succeeded.

Get index information with indexInformation()

indexInformation can be used to fetch some useful information about collection indexes.

collection.indexInformation(callback)

Where callback gets two parameters - an error object (if an error occured) and an index information object.

The keys in the index object are the index names and the values are tuples of included fields.

For example if a collection has two indexes - as a default an ascending index for the _id field and an additonal descending index for "username" field, then the index information object would look like the following

javascript   {     "_id":[["_id", 1]],     "username_-1":[["username", -1]]   }


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4