---
title: NoSQL database
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/nosql/
updated: 2026-11-19T16:17:12.159Z
---

# NoSQL database

> NOSQL(name), its query builder methods and the NoSQL engine of DATA.

Methods of `NOSQL(name)`, the query builder they return (filters, fields, sorting, paging and results), the NoSQL reads of `DATA` and the `$dirdatabases` configuration key. For an introduction with examples read the guide [NoSQL database](../../guides/nosql/).

## API

### NOSQL(name)

Type: `method`

Returns a NoSQL database stored in a text file.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Name of the database, or a file path with a slash. |

Returns: `Object` An object with `insert()`, `find()`, `read()`, `update()`, `modify()`, `remove()`, `clear()` and `clean()`.

A plain name uses the file `databases/<name>.nosql`; the directory and the file are created when needed. A name with a slash is used as the file path as it is. Every method of the returned object returns a query builder.

Example: Use a database
```js
var db = NOSQL('users');
db.find().where('active', true).callback(console.log);
```

### NOSQL(name).insert(doc)

Type: `method`

Appends a new document.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `doc` | `Object` | yes |  | The document. |

Returns: `QueryBuilder` Builder; the response is `1`.

Adds the document as a new line at the end of the file. No `id` is generated, so add your own. Keys with the prefixes `+ - * / > <` store the value without the prefix, `!key` stores `true` and `=key` stores the value as it is. The response is `1`.

Example: Insert
```js
NOSQL('users').insert({ id: UID(), name: 'Peter', dtcreated: NOW }).callback(function(err, response) {
	// response === 1
});
```

### NOSQL(name).find()

Type: `method`

Finds documents.

Returns: `QueryBuilder` Builder; the response is an Array.

Returns all matching documents as an array, or an empty array. Without `sort()` the most recently written documents come first.

Example: Find
```js
NOSQL('users').find().where('age', '>', 18).sort('name').take(10).callback(function(err, items) {
	console.log(items);
});
```

### NOSQL(name).read()

Type: `method`

Reads one document.

Returns: `QueryBuilder` Builder; the response is an Object or `undefined`.

Returns the first matching document (the most recently written one when you do not sort), or `undefined` when nothing matches. Combine it with `error()` to answer with an error for a missing document.

Example: Read by id
```js
NOSQL('users').read().id(id).error('Not found').callback($);
```

### NOSQL(name).update(data)

Type: `method`

Updates matching documents.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object` | yes |  | Fields to write, optionally with a prefix. |

Returns: `QueryBuilder` Builder; the response is the count of updated documents.

Writes the keys of `data` into every matching document; other fields are kept. Without a filter all documents are updated. Key prefixes: `+key`, `-key`, `*key` and `/key` add, subtract, multiply or divide the field by the number (a missing field counts as `0`), `>key` writes the value only when it is greater than the field and `<key` only when it is smaller (a missing field takes the value), `!key` toggles a boolean, `=key` evaluates a JavaScript expression with `doc`. The response is the number of updated documents.

Example: Update
```js
NOSQL('products').update({ '+stock': -1, dtupdated: NOW }).id(id).callback(function(err, count) {
	console.log(count);
});
```

Example: Counters and limits
```js
NOSQL('products').update({ '-stock': 1, '*price': 1.1, '>maxorder': 3 }).id(id).callback(console.log);
```

### NOSQL(name).modify(data)

Type: `method`

Alias of `update()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object` | yes |  | Fields to write, optionally with a prefix. |

Returns: `QueryBuilder` Builder; the response is the count of updated documents.

Writes the keys of `data` into every matching document; other fields are kept. Without a filter all documents are updated. Key prefixes: `+key`, `-key`, `*key` and `/key` add, subtract, multiply or divide the field by the number (a missing field counts as `0`), `>key` writes the value only when it is greater than the field and `<key` only when it is smaller (a missing field takes the value), `!key` toggles a boolean, `=key` evaluates a JavaScript expression with `doc`. The response is the number of updated documents.

Example: Modify
```js
NOSQL('users').modify({ '!active': 1 }).id(id).callback(console.log);
```

### NOSQL(name).remove()

Type: `method`

Removes matching documents.

Returns: `QueryBuilder` Builder; the response is the count of removed documents.

Marks every matching document as removed. Without a filter all documents are removed. The response is the count of removed documents. The lines stay in the file until `clean()`.

Example: Remove old logs
```js
NOSQL('logs').remove().where('dtcreated', '<', NOW.add('-30 days')).callback(console.log);
```

### NOSQL(name).clear()

Type: `method`

Deletes all documents.

Returns: `QueryBuilder` Builder.

Deletes the database file. The next insert creates it again.

Example: Clear
```js
NOSQL('cache').clear().callback(function(err) {
	console.log('empty');
});
```

### NOSQL(name).clean()

Type: `method`

Compacts the database file.

Returns: `QueryBuilder` Builder.

Rewrites the file without the lines of removed documents and of old versions of updated documents. Writes wait until it finishes.

Example: Clean once a day
```js
ON('service', function(counter) {
	if (counter % 1440 === 0)
		NOSQL('logs').clean();
});
```

### builder.where(name, [comparer], value)

Type: `method`

Compares a field with a value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |
| `comparer` | `String` | no | `'='` | Comparison operator. |
| `value` | `Object` | yes |  | Value to compare. |

Returns: `QueryBuilder` The same builder, for chaining.

Comparers: `=` (default), `==`, `<>`, `!=`, `>`, `>=`, `<`, `<=`. `=` compares loosely, so `'5'` matches `5`. The name can be nested (`address.city`). With only one argument it works as `query()`.

Example: Where
```js
NOSQL('users').find().where('age', '>=', 18).where('address.city', 'Bratislava').callback(console.log);
```

### builder.query(code)

Type: `method`

Adds a raw JavaScript condition.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `code` | `String` | yes |  | JavaScript expression. |

Returns: `QueryBuilder` The same builder, for chaining.

The condition can use `doc`, the document. A condition that contains `require` or `global` matches nothing. Never build it from user input.

Example: Raw condition
```js
NOSQL('orders').find().query('doc.items.length > 2').callback(console.log);
```

### builder.equal(obj)

Type: `method`

Adds `where(key, value)` for every key of an object.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | Fields and values. |

Returns: `QueryBuilder` The same builder, for chaining.

Each key of the object must equal its value.

Example: Equal
```js
NOSQL('users').find().equal({ role: 'admin', active: true }).callback(console.log);
```

### builder.id(id)

Type: `method`

Filters by the `id` field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String|Array` | yes |  | Identifier or identifiers. |

Returns: `QueryBuilder` The same builder, for chaining.

A value adds `where('id', id)`, an array adds `in('id', id)`.

Example: By id
```js
NOSQL('users').read().id($.params.id).callback($);
```

### builder.userid(id)

Type: `method`

Filters by the `userid` field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String|Array|Object` | yes |  | Identifier, identifiers, a user object or `$`. |

Returns: `QueryBuilder` The same builder, for chaining.

Accepts a string, an array of identifiers, an object with `id`, or `$` (it then uses `$.user.id`).

Example: Documents of the user
```js
NOSQL('notes').find().userid($).callback($);
```

### builder.in(name, value, [key])

Type: `method`

The field is one of the values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |
| `value` | `Array` | yes |  | Allowed values. |
| `key` | `String` | no |  | Property to read from objects in `value`. |

Returns: `QueryBuilder` The same builder, for chaining.

A single value is wrapped in an array. When the field is an array, a document matches if any of its items is one of the values. With `key`, the values are taken from that property of the objects in `value`.

Example: In
```js
NOSQL('products').find().in('tags', ['usb', 'wireless']).callback(console.log);
```

### builder.notin(name, value, [key])

Type: `method`

The field is none of the values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |
| `value` | `Array` | yes |  | Forbidden values. |
| `key` | `String` | no |  | Property to read from objects in `value`. |

Returns: `QueryBuilder` The same builder, for chaining.

The opposite of `in()`, with the same arguments.

Example: Not in
```js
NOSQL('users').find().notin('role', ['guest', 'banned']).callback(console.log);
```

### builder.between(name, a, b)

Type: `method`

The field is between two values, inclusive.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |
| `a` | `Number|Date` | yes |  | Lower bound. |
| `b` | `Number|Date` | yes |  | Upper bound. |

Returns: `QueryBuilder` The same builder, for chaining.

Works with numbers, strings and dates.

Example: Between
```js
NOSQL('orders').find().between('price', 10, 100).callback(console.log);
```

### builder.search(name, value, [where])

Type: `method`

Searches text in a field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |
| `value` | `String` | yes |  | Text to find. |
| `where` | `String` | no |  | `'beg'` or `'end'`. |

Returns: `QueryBuilder` The same builder, for chaining.

Case-insensitive. By default the text can be anywhere; `'beg'` matches the beginning and `'end'` the end. An array field matches when any item matches. `%` characters in the text are removed.

Example: Search
```js
NOSQL('users').find().search('email', '@example.com', 'end').callback(console.log);
```

### builder.contains(name)

Type: `method`

The field has a value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |

Returns: `QueryBuilder` The same builder, for chaining.

Matches a truthy value, or an array with at least one item.

Example: Contains
```js
NOSQL('users').find().contains('photo').callback(console.log);
```

### builder.empty(name)

Type: `method`

The field has no value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |

Returns: `QueryBuilder` The same builder, for chaining.

Matches a missing or falsy value, or an empty array.

Example: Empty
```js
NOSQL('products').find().empty('tags').callback(console.log);
```

### builder.or(fn)

Type: `method`

Joins a group of filters with OR.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | function(builder) that adds the filters. |

Returns: `QueryBuilder` The same builder, for chaining.

The function receives the builder (also as `this`); filters added inside it form one group where any of them can match.

Example: Or
```js
NOSQL('users').find().or(function(builder) {
	builder.where('role', 'admin');
	builder.where('owner', true);
}).callback(console.log);
```

### builder.year(name, [comparer], value)

Type: `method`

Compares the full year of a Date field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Date field. |
| `comparer` | `String` | no | `'='` | Comparison operator. |
| `value` | `Number` | yes |  | Value to compare. |

Returns: `QueryBuilder` The same builder, for chaining.

Documents where the field is not a `Date` do not match. Comparers are the same as in `where()`.

Example: Year
```js
NOSQL('orders').find().year('dtcreated', 2024).callback(console.log);
```

### builder.month(name, [comparer], value)

Type: `method`

Compares the month (1-12) of a Date field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Date field. |
| `comparer` | `String` | no | `'='` | Comparison operator. |
| `value` | `Number` | yes |  | Value to compare. |

Returns: `QueryBuilder` The same builder, for chaining.

Documents where the field is not a `Date` do not match. Comparers are the same as in `where()`.

Example: Month
```js
NOSQL('orders').find().month('dtcreated', '>=', 6).callback(console.log);
```

### builder.day(name, [comparer], value)

Type: `method`

Compares the day of the month (1-31) of a Date field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Date field. |
| `comparer` | `String` | no | `'='` | Comparison operator. |
| `value` | `Number` | yes |  | Value to compare. |

Returns: `QueryBuilder` The same builder, for chaining.

Documents where the field is not a `Date` do not match. Comparers are the same as in `where()`.

Example: Day
```js
NOSQL('orders').find().day('dtcreated', 1).callback(console.log);
```

### builder.fields(fields)

Type: `method`

Chooses the returned fields.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fields` | `String` | yes |  | Comma-separated field names. |

Returns: `QueryBuilder` The same builder, for chaining.

A comma-separated list of fields to return, or of fields with `-` to leave out (`'-password,-token'`).

Example: Fields
```js
NOSQL('users').find().fields('id,name,email').callback(console.log);
```

### builder.sort(name, [desc])

Type: `method`

Sorts the result.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Field name. |
| `desc` | `Boolean|String` | no | `false` | `true` or `'desc'` for descending order. |

Returns: `QueryBuilder` The same builder, for chaining.

Call it more times to sort by more fields. Nested fields work (`address.city`). Without sorting the most recently written documents come first.

Example: Sort
```js
NOSQL('articles').find().sort('dtcreated', true).sort('title').callback(console.log);
```

### builder.take(count)

Type: `method`

Limits the number of documents.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | Maximum count. |

Returns: `QueryBuilder` The same builder, for chaining.

Reading stops as soon as enough documents are found, unless the result is sorted.

Example: Take
```js
NOSQL('logs').find().take(50).callback(console.log);
```

### builder.limit(count)

Type: `method`

Alias of `take()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | Maximum count. |

Returns: `QueryBuilder` The same builder, for chaining.

Limits the number of documents.

Example: Limit
```js
NOSQL('logs').find().limit(50).callback(console.log);
```

### builder.skip(count)

Type: `method`

Skips documents.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `count` | `Number` | yes |  | Count to skip. |

Returns: `QueryBuilder` The same builder, for chaining.

Skips the first `count` matching documents.

Example: Skip
```js
NOSQL('logs').find().skip(100).take(50).callback(console.log);
```

### builder.first()

Type: `method`

Returns only one document.

Returns: `QueryBuilder` The same builder, for chaining.

Sets `take(1)` and the response is an object (or `undefined`) instead of an array.

Example: First
```js
NOSQL('users').find().where('email', email).first().callback(console.log);
```

### builder.page(page, [limit])

Type: `method`

Sets skip from a page number.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `page` | `Number` | yes |  | Page number from 1. |
| `limit` | `Number` | no |  | Documents per page. |

Returns: `QueryBuilder` The same builder, for chaining.

Page numbers start at 1. `limit` sets `take()`; without it the current `take()` value is used.

Example: Page
```js
NOSQL('articles').find().sort('dtcreated', true).page(2, 20).callback(console.log);
```

### builder.paginate(page, limit, [maxlimit])

Type: `method`

Sets skip and take from untrusted values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `page` | `Number|String` | yes |  | Page number from 1. |
| `limit` | `Number|String` | yes |  | Documents per page. |
| `maxlimit` | `Number` | no |  | Maximum and default limit. |

Returns: `QueryBuilder` The same builder, for chaining.

Accepts strings (for example from the query string). A page below 1 becomes 1; `limit` is capped at `maxlimit`, and a missing limit uses `maxlimit`.

Example: Paginate in an action
```js
NOSQL('articles').find().sort('dtcreated', true).paginate($.query.page, $.query.limit, 50).callback($);
```

### builder.debug()

Type: `method`

Prints the compiled operation.

Returns: `QueryBuilder` The same builder, for chaining.

Logs the file, the operation and the compiled filter to the console with the prefix `-- NoSQL -->`.

Example: Debug
```js
NOSQL('users').find().where('age', '>', 18).debug().callback(console.log);
```

### builder.callback(fn)

Type: `method`

Reads the result with a callback.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function|Object` | yes |  | function(err, response), or `$`. |

Returns: `QueryBuilder` The same builder, for chaining.

`fn(err, response)` is called when the operation finishes. Pass `$` in an action to send the response or the error to the client. `pipe()` is an alias.

Example: Callback
```js
NOSQL('users').find().callback(function(err, items) {
	if (err)
		return console.error(err);
	console.log(items.length);
});
```

### builder.pipe(fn)

Type: `method`

Alias of `callback()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function|Object` | yes |  | function(err, response), or `$`. |

Returns: `QueryBuilder` The same builder, for chaining.

`fn(err, response)`, or `$`.

Example: Pipe
```js
NOSQL('users').find().pipe($);
```

### builder.promise([$])

Type: `method`

Returns the result as a Promise.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | no |  | Options of an action. |

Returns: `Promise` Resolves with the response.

The Promise resolves with the response. On an error it rejects, or, when `$` is given, the error is sent to `$.invalid()`.

Example: Await
```js
var items = await NOSQL('users').find().where('active', true).promise($);
```

### builder.done($, fn, [param])

Type: `method`

Calls a function with the response, errors go to `$`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | Options of an action. |
| `fn` | `Function` | yes |  | function(response, param). |
| `param` | `Object` | no |  | A value passed to `fn`. |

Returns: `QueryBuilder` The same builder, for chaining.

On success `fn(response, param)` is called; an error is sent to `$.invalid()`.

Example: Done
```js
NOSQL('users').read().id(id).error('Not found').done($, function(user) {
	$.callback({ name: user.name });
});
```

### builder.data(fn)

Type: `method`

Calls a function only with a response.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | function(response). |

Returns: `QueryBuilder` The same builder, for chaining.

`fn(response)` is called only when there is no error. It can be combined with `callback()`.

Example: Data
```js
NOSQL('users').find().data(function(items) {
	console.log(items);
});
```

### builder.fail(fn)

Type: `method`

Calls a function only with an error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | function(err). |

Returns: `QueryBuilder` The same builder, for chaining.

`fn(err)` is called only when the operation fails.

Example: Fail
```js
NOSQL('users').read().id(id).error('Not found').fail(function(err) {
	console.log(err);
});
```

### builder.error(message, [reverse])

Type: `method`

Turns an empty result into an error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String|Number` | yes |  | Error message. |
| `reverse` | `Boolean` | no | `false` | Fail when the result is not empty. |

Returns: `QueryBuilder` The same builder, for chaining.

The operation fails with `message` when the response is empty: `undefined`, an empty array, or `0` updated or removed documents. With `reverse` it fails when the response is not empty, for example to stop a duplicate. `err()` is an alias.

Example: Not found and duplicate
```js
NOSQL('users').read().id(id).error('Not found').callback($);

NOSQL('users').read().where('email', email).error('E-mail is already used', true).callback(function(err) {
	// err when the e-mail exists
});
```

### DATA.count('nosql/' + name)

Type: `method`

Counts matching documents.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Database name with the `nosql/` prefix. |

Returns: `QueryBuilder` Builder; the response is `{ count }`.

Uses the NoSQL engine through the global query builder. The response is an object `{ count: Number }`.

Example: Count
```js
DATA.count('nosql/users').where('active', true).callback(function(err, response) {
	console.log(response.count);
});
```

### DATA.check('nosql/' + name)

Type: `method`

Checks if a document exists.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Database name with the `nosql/` prefix. |

Returns: `QueryBuilder` Builder; the response is a Boolean.

Stops at the first matching document. The response is `true` or `false`.

Example: Check
```js
DATA.check('nosql/users').where('email', email).callback(function(err, exists) {
	console.log(exists);
});
```

### DATA.scalar('nosql/' + name, type, key, [key2])

Type: `method`

Computes a sum, minimum, maximum, count or groups.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Database name with the `nosql/` prefix. |
| `type` | `String` | yes |  | `'sum'`, `'min'`, `'max'`, `'avg'`, `'count'` or `'group'`. |
| `key` | `String` | no | `'*'` | Field to compute or to group by. `'*'` (the default) counts each document as `1`. |
| `key2` | `String` | no |  | Field to sum per group (only with `'group'`). |

Returns: `QueryBuilder` Builder; the response is `{ value }` or an Array of groups.

With `type` `'sum'`, `'min'`, `'max'`, `'avg'` or `'count'` the response is `{ value }` for the field `key` (documents without the field are skipped; `min` and `max` work with dates too). `avg` is the sum divided by the count, or `null` when no document matches. Without `key` (or with `'*'`) every matching document counts as `1`, so `DATA.scalar('nosql/orders', 'count')` counts the documents. With `'group'` the response is an array of `{ <key>: value, value: count }`; with `key2` the values of `key2` are summed per group instead of counted.

Example: Sum
```js
DATA.scalar('nosql/orders', 'sum', 'price').where('paid', true).callback(function(err, response) {
	console.log(response.value);
});
```

Example: Group
```js
DATA.scalar('nosql/orders', 'group', 'status').callback(function(err, groups) {
	// [{ status: 'paid', value: 12 }, { status: 'new', value: 3 }]
});
```

Example: Average and count
```js
DATA.scalar('nosql/orders', 'avg', 'price').callback(function(err, response) {
	console.log(response.value); // the average price
});

DATA.scalar('nosql/orders', 'count').where('paid', true).callback(console.log);
```

### $dirdatabases : String

Type: `config`

Directory of the database files.

Returns: `String`

The directory where `NOSQL()` keeps its `.nosql` files. The default is `databases` in the root of the application.

Example: config
```text
$dirdatabases : /var/data/myapp/
```
