Mikuláš Sychra
Mikuláš Sychra

Reputation: 21

listening to pouchdb changes in particular view returns all documents

Trying to retrieve changes in pouchDB for particular view, but still receiving allDocs ... what am I doing wrong? Havent found any tutorials or detailed info how to do this, except pouch API doc (http://pouchdb.com/api.html#changes). I've tested view and filter options with no luck.

This view is designed and saved as /notes

function(doc) {
  if (doc.model == 'note') {
    emit(doc._id, doc);
  }
}

live changes feed:

pouch.changes({ 
  live: true,
  view: '_view/notes'
})
.on('change', function handleUpdate(change) {

  // log changed document
  pouch.get(change.id).then(function(row) {
    console.log(row);
  }

})

Maybe the view path is wrong...? Thanks for any help.

Upvotes: 2

Views: 2327

Answers (2)

PhE
PhE

Reputation: 16644

BY default changes() will return all the changes from the beginning of the database. So you will receive ALL documents change, which is equivalent to a call to allDocs().

To get the changes from a certain point in time, you have to give the option parameter since.

From PouchDB api doc:

options.since: Start the results from the change immediately after the
given sequence number, you can also pass 'now' if you want only
new changes.

You can get a sequence number from a call to db.info() or when retrieving a document via db.get(docId) with the option local_seq.

Upvotes: 0

nlawson
nlawson

Reputation: 11620

Much more simply, you can do:

pouch.changes({ 
  live: true,
  include_docs: true
}).on('change', function (change) {
  if (change.doc && change.doc.model === 'note') {
    console.log(change);
  }
});

Trust me; using views in a changes() filter is just adding unnecessary complexity and is not going to perform faster. Sorry for not making that clear in the documentation. :)

Upvotes: 1

Related Questions