Start a new topic

How to filter documents within dates?

Hi, I'm trying to query a collection by a 'date'  property within two dates (the 'midnight' and 'now').

If I filter 'date' with a single operator ($lte or $gte) it find documents. But when I use both operators, $lte and $gte, it doesn't find any documents.

This is part of the code related to a custom endpoint.



function onRequest(request, response, modules) {
  var now = ('date' in request.body) ? new Date( : new Date(),
      midnight = new Date(now);
  var query = {
      date: {
        $gte: midnight.toISOString(),
        $lte: now.toISOString()
  var logs = modules.collectionAccess.collection('some-logs');
  .then(function(d) {
    if (!d) {
      // I always get here, even when I have documents
      // that are from this day

  }, modules.logger.error);



 The 'date' is stored as a ISO8601 string (for example, "2016-04-25T10:52:18.020Z"). Any help would be very appreciated.

Hi Carlos, to get a better insight into what could be going wrong, I recommend logging the query using modules.logger to see if it's what you expect.

If that doesn't help you figure out the issue, can you reply to this post with both the logged query, as well as an example of an entity you know is in your collection and that you expect the query to fetch?
Ok, this is an entity from the collection 'some-logs':




e1 = { "date": "2016-04-25T23:20:28.129Z", "log": [ { "takenAt": "2016-04-25T23:20:28.129Z" } ], "_id": "571ea63c409b6011005bd95a", "_acl": { "creator": "some_creator" }, "_kmd": { "ect": "2016-04-25T23:20:28.158Z", "lmt": "2016-04-25T23:20:28.158Z" } }


 Let's assume 'now' is "2016-04-25T23:30:00.000Z", and because of that, 'midnight' is "2016-04-25T00:00:00.000Z".

So because '' is "2016-04-25T23:20:28.129Z" and

  • '' <= "2016-04-25T23:00:00.000Z"
  • "2016-04-25T00:00:00.000Z" <= ''

I would expect that the query in 'findOneAsync' returns 'e'. But it returns null.

What I am actually doing is trying to update the 'log' property of an entity made today. If the entity doesn’t exist, we create it right away. We then save the changes (or store the new entity) with 'logs.saveAsync(d);'.

The problem is that when I run the code twice, the first time in the day the entity is created... and the second time a new entity is created, when I expect that 'findOneAsync' had found my first entity.

Login or Signup to post a comment