How to get all documents in a specific range of time?

336 Views Asked by At

Like said in the title, im trying to get all docs in a specific time range. I have documents in my database with a timestamp stored. I was looking for something like here Querying CouchDB documents between a start date and an end date but i didnt really get it working for me, i also use nano for nodejs and i dont know how to write something like

?startkey="1970-01-01T00:00:00Z"&endkey="1971-01-01T00:00:00Z"

in nano syntax. I was looking for something here https://www.npmjs.com/package/nano#dbviewdesignname-viewname-params-callback but i couldnt find something similair.

1

There are 1 best solutions below

1
uminder On BEST ANSWER

You can use /db/_find with a selector that uses operators $gte and $lt (or $lte) described in the chapter Condition Operators of the CouchDB API Reference.

"selector": {
   "myTimestamp": {
      "$gte": "2020-01-23T06:00:00.000Z",
      "$lt": "2020-01-23T07:00:00.000Z"
   }
}

With nano db.find, this could look as follows:

const q = {
  selector: {
    myTimestamp: {
      "$gte": "2020-01-23T06:00:00.000Z",
      "$lt": "2020-01-23T07:00:00.000Z"
    }
  }
};

myDb.find(q).then((doc) => {
   console.log(doc);
});