Update and remove NoSQL documents
update(data) and remove() work like find(): add the conditions with .where() and finish with .callback(function(err, count) {}). The callback receives the number of documents that were changed or removed. Always add a condition: without one the operation applies to every document.
update(data) merges the fields of data into the matching documents. Use read() to get a single document.
exports.install = function() {
ROUTE('POST /tasks/', create);
ROUTE('PATCH /tasks/{id}/', done);
ROUTE('DELETE /tasks/{id}/', remove);
ROUTE('GET /tasks/{id}/', read);
};
function create($) {
var task = { id: $.body.id, title: $.body.title, done: false };
NOSQL('tasks').insert(task).callback(() => $.json(task));
}
function done($) {
NOSQL('tasks').update({ done: true }).where('id', $.params.id).callback((err, count) => $.json({ updated: count }));
}
function remove($) {
NOSQL('tasks').remove().where('id', $.params.id).callback((err, count) => $.json({ removed: count }));
}
function read($) {
NOSQL('tasks').read().where('id', $.params.id).callback((err, response) => $.json(response || { missing: true }));
}