The JavaScript Database, for Node.js, nw.js, electron and the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
nedb/docs/Index.md

6.9 KiB

Index

Indexes on field names, with atomic operations and which can optionally enforce a unique constraint or allow indexed fields to be undefined

Kind: global class

new Index(options)

Create a new index All methods on an index guarantee that either the whole operation was successful and the index changed or the operation was unsuccessful and an error is thrown while the index is unchanged

Param Type Default Description
options object
options.fieldName string

On which field should the index apply (can use dot notation to index on sub fields)

[options.unique] boolean false

Enforces a unique constraint

[options.sparse] boolean false

Allows a sparse index (we can have documents for which fieldName is undefined)

index.fieldName : string

On which field the index applies to (may use dot notation to index on sub fields).

Kind: instance property of Index

index.unique : boolean

Defines if the index enforces a unique constraint for this index.

Kind: instance property of Index

index.sparse : boolean

Defines if we can have documents for which fieldName is undefined

Kind: instance property of Index

index.treeOptions : Object

Options object given to the underlying BinarySearchTree.

Kind: instance property of Index

index.tree : AVLTree

Underlying BinarySearchTree for this index. Uses an AVLTree for optimization.

Kind: instance property of Index

index.reset([newData])

Reset an index

Kind: instance method of Index

Param Type Description
[newData] document | ?Array.<document>

Data to initialize the index with. If an error is thrown during insertion, the index is not modified.

index.insert(doc)

Insert a new document in the index If an array is passed, we insert all its elements (if one insertion fails the index is not modified) O(log(n))

Kind: instance method of Index

Param Type Description
doc document | Array.<document>

The document, or array of documents, to insert.

index.remove(doc)

Removes a document from the index. If an array is passed, we remove all its elements The remove operation is safe with regards to the 'unique' constraint O(log(n))

Kind: instance method of Index

Param Type Description
doc Array.<document> | document

The document, or Array of documents, to remove.

index.update(oldDoc, [newDoc])

Update a document in the index If a constraint is violated, changes are rolled back and an error thrown Naive implementation, still in O(log(n))

Kind: instance method of Index

Param Type Description
oldDoc document | Array.<{oldDoc: document, newDoc: document}>

Document to update, or an Array of {oldDoc, newDoc} pairs.

[newDoc] document

Document to replace the oldDoc with. If the first argument is an Array of {oldDoc, newDoc} pairs, this second argument is ignored.

index.revertUpdate(oldDoc, [newDoc])

Revert an update

Kind: instance method of Index

Param Type Description
oldDoc document | Array.<{oldDoc: document, newDoc: document}>

Document to revert to, or an Array of {oldDoc, newDoc} pairs.

[newDoc] document

Document to revert from. If the first argument is an Array of {oldDoc, newDoc}, this second argument is ignored.

index.getMatching(value) ⇒ Array.<document>

Get all documents in index whose key match value (if it is a Thing) or one of the elements of value (if it is an array of Things)

Kind: instance method of Index

Param Type Description
value Array.<*> | *

Value to match the key against

index.getBetweenBounds(query) ⇒ Array.<document>

Get all documents in index whose key is between bounds are they are defined by query Documents are sorted by key

Kind: instance method of Index

Param Type Description
query object

An object with at least one matcher among $gt, $gte, $lt, $lte.

[query.$gt] *

Greater than matcher.

[query.$gte] *

Greater than or equal matcher.

[query.$lt] *

Lower than matcher.

[query.$lte] *

Lower than or equal matcher.

index.getAll() ⇒ Array.<document>

Get all elements in the index

Kind: instance method of Index