pouchdbcouchdb-mango

Pouchdb-find docs from the last 30 days


I'm trying to perform a $gte query on a PouchDB index I've created. The query doesn't error but returns an empty array when there should be documents that match the clause. The idea is to get all docs that have timestamps from the last 30 days. The property that stores that value is not at the top level, it is part of a meta property. eg.

{
    _id: "76e7f205-b8c3-4cc6-abfb-77c98ca3a0d4",
    _rev: "1-47f49534aa364f5c654b9574fa085005",
    ...
    meta: {
        created: 1501841768486
        createdBy: "user@example.com"
    }
}

I create my index successfully using the following:

recordingsDB.createIndex({
    name: 'recent',
    ddoc: 'recent',
    index: {
        fields: ['meta.created']
    }
});

I then query it using:

let thirtyDaysAgo = moment(Date.now()).subtract(30, 'days').format('x');

//selector: { 'meta.created': { $gte: thirtyDaysAgo } },

return recordingsDB.find({
    selector: { meta: { created: { $gte: thirtyDaysAgo } } },
    sort: [{'meta.created': 'desc'}],
    use_index: 'recent'
}).catch(function (err) {
    console.log(err);
});

I have tried both syntax styles for querying sub-properties and neither work. The query doesn't actually fail or cause any errors - it just returns an empty array when I know there are documents that should match that query.


Solution

  • You'll need to wrap thirtyDaysAgo in Number() as it is a string while your created date is a number. The type difference is significant to pouchdb-find since it is using the same underlying index standard as couchdb, and these have a type based collation order.