Uppdatera och ta bort NoSQL-dokument
update(data) och remove() fungerar som find(): lägg till villkoren med .where() och avsluta med .callback(function(err, count) {}). Callbacken får antalet dokument som ändrades eller togs bort. Lägg alltid till ett villkor: utan det gäller operationen varje dokument.
update(data) slår ihop fälten i data med de matchande dokumenten. Använd read() för att hämta ett enskilt dokument.
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 }));
}