# Atualizar e remover documentos NoSQL

> Altere documentos com update() e where(), apague-os com remove() e leia quantos foram afetados.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: pt
Content-SHA256: fd1fb87240809b19506412b102fdf62c9a22c9bf1a379697eb08c80f4316bb1b
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`update(data)` e `remove()` funcionam como `find()`: acrescente as condições com `.where()` e termine com `.callback(function(err, count) {})`. O callback recebe o número de documentos alterados ou removidos. **Acrescente sempre uma condição**: sem ela a operação aplica-se a todos os documentos.

`update(data)` junta os campos de `data` aos documentos correspondentes. Use `read()` para obter um único documento.

```js controllers/tasks.js
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 }));
}
```
