Database Operations
Writing, reading, updating, deleting, counting, indexing, reloading and compacting.
Database.insert
Database.insert
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the inserted documents.number
: Is the number of the inserted documents.
Database.find
Database.find
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
skip
andlimit
are both number, and basically self-explanatory.sort
is an object that its keys must be the same asModel
keys (or some of them), yet the value of those keys must be either1
(for ascending sorting) or-1
(for descending sorting).
You can also use the computed value when filtering (the beauty of object mapping).
For more about query operators and options read: Query API documentation. And for more about the benifit of object mapping in the query API, read: Object Mapping documentation.
Return type
Method return a promise fulfilling with an array of the matched documents. If no documents matches then the array would be empty.
Database.update
Database.update
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
update
is an object of update operators and modifiers, similar to the update operators and modifiers of MonogDB.multi
is aboolean
, defaults to false, if true it will update all the matched documents, if it's false it will update only the first matched document.
For more about query operators and options read: Query API documentation. And for more about update operators and modifiers read: Update API documentation.
Do not apply update operators to the readonly
values that are the computed properties (e.g. age property in the model above).
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the update documents.number
: Is the number of the updated documents.
Database.upsert
Database.upsert
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
update
is an object of update operators and modifiers, similar to the update operators and modifiers of MonogDB. But with one required field ($setOnInsert
). This field value must the document to be inserted if no documents where found to be updates.multi
is aboolean
, defaults to false, if true it will update all the matched documents, if it's false it will update only the first matched document.
For more about query operators and options read: Query API documentation. And for more about update operators and modifiers read: Update API documentation.
Do not apply update operators to the readonly
values that are the computed properties (e.g. age property in the model above).
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the inserted/updated documents.number
: Is the number of the inserted/updated documents.upsert:
is a boolean that should betrue
when an insertion actually occurred, and should befalse
if an update occurred.
Database.count
Database.count
For counting documents that meet a specified query. This method takes the Query<Model>
as an argument. It is similar to the query language of MongoDB, it can accepts direct equality evaluation like {age: 11}
or a field level operator: {age: { $gt: 11 }}
or a top level operator: $or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
Return type
Method return a promise fulfilling with a number, that is the number of the documents matching the given query.
Database.delete
Database.delete
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
multi
is aboolean
, defaults to false, if true it will update all the matched documents, if it's false it will update only the first matched document.
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the deleted documents.number
: Is the number of the deleted documents.
Database.createIndex
Database.createIndex
fieldName
(required): name of the field to index.unique
(optional, defaults tofalse
): enforce field uniqueness. Note that a unique index will raise an error if you try to index two documents for which the field is not defined.sparse
(optional, defaults tofalse
): don't index documents for which the field is not defined. Use this option along with "unique" if you want to accept multiple documents for which it is not defined.expireAfterSeconds
(number of seconds, optional): if set, the created index is a TTL (time to live) index, that will automatically remove documents when the system date becomes larger than the date on the indexed field plusexpireAfterSeconds
. Documents where the indexed field is not specified or not aDate
object are ignored.
Return type
Method return a promise fulfilling with object that has the following properties:
affectedIndex
: Is a string of the field name that the index created upon.
Database.removeIndex
Database.removeIndex
Return type
Method return a promise fulfilling with object that has the following properties:
affectedIndex
: Is a string of the field name that the index created upon.
Database.reload
Database.reload
Reloading the persistence layer (e.g. the file) of the database, so any changes that were made external to the instance of the database will be loaded into this instance.
Database.forcefulUnlock
Database.forcefulUnlock
There's a locking mechanism implemented in the file system persistence adapter to prevent two instances from modifying the same file at once. So one of them has to wait for the other. However, it may occur that one of the instances would crash while modifying the file and the lock would still be there, so the other instance would have to wait forever for the file to be unlocked.
This is why this utility method is provided as a means of forcefully unlocking the file.
Database.compact
Database.compact
When persisting data (e.g. into a file), the data will be persisted by appending, this is to avoid re-writing of the whole file in case of removing or updating. This behavior might result in an increase in the persistence layer size (e.g. file size). That's why the database provides you with a function to compact the file size:
Database.resetAutoCompaction
Database.resetAutoCompaction
Resetting (or starting) auto compaction at intervals of time.
Database.stopAutoCompaction
Database.stopAutoCompaction
Stopping auto compaction.
Last updated