0

Edited for better explanation:

i have a lot of entries throughout all the day

Documents in my collection are like this:

{
"type": "Watches",
"timestamp": ISODate("2016-02-23T21:00:00.000Z"),
"price": 100.77
}
{
"type": "Watches",
"timestamp": ISODate("2016-02-23T20:50:00.000Z"),
"price": 200.77
}
{
"type": "Watches",
"timestamp": ISODate("2016-02-24T21:00:00.000Z"),
"price": 300.77
}
{
"type": "Watches",
"timestamp": ISODate("2016-02-24T20:51:00.000Z"),
"price": 94.77
}

The entries are saved in minutes. But, some days there are NOT entries at all.

So, how can i search all the entries at 21:00 until get 10 results? (skipping the days that there are not entries)

my first try was making an array like:

  var array_of_dates =[ Thu Feb 24 2016 21:00:00 GMT-0300 (ART),
                        Wed Feb 23 2016 21:00:00 GMT-0300 (ART) ]

and use it with { '$in' : array_of_dates } but, if some days that there are not entris, i get less than 10 results.

Liza Daly
  • 2,933
  • 23
  • 31
MatCas
  • 773
  • 8
  • 19
  • 1
    So you would query for a given date and a specific hour? can you please edit your question to include a sample inputs for your query. – BatScream Feb 26 '16 at 23:20
  • 1
    In aggreance with @BatScream here as your intent is not very clear, and the title seems at odds with the general body of the question. You are possibly asking for the *"last 20 entries before 8am on each day, in 'set' combination"*, but that's just a guess at interpretaion. MongoDB does "still" ( currently ) lack the necessary operators to make such a collection of data efficient in a single "query" operation, and at least for "large" datasets. Your best approach is therefore still to run multiple queries, ideally in parallel and combine the results. – Blakes Seven Feb 26 '16 at 23:58
  • @BatScream can you understand now?. – MatCas Feb 27 '16 at 01:06
  • @BlakesSeven do you have an example how to make multiples queries in parallel and combine the results? i use nodejs, and the Q library, you know it? – MatCas Feb 27 '16 at 01:08
  • Could you specify the interval like from 21:00 to xx:xx or is it that you want to find all entries before/after 21:00 for all days. I am sorry but the question is not that clear for me. – Nidhin David Feb 27 '16 at 17:23
  • @NidhinDavid i want to search each X time period, like, 10 results of the last 10 minutes, or 10 results of the last 10 days – MatCas Feb 28 '16 at 23:36

1 Answers1

0

About

So, how can i search all the entries at 21:00 until get 10 results?

This answer can be helpful: https://stackoverflow.com/a/2943685/3359432

In MongoDB you can use $lt, $gt etc on date fields. Make sure you create a Date object that you want to compare and pass it to the find query. Have a look at the code...

Example query in Mongo Shell:

db.collection('myCollection').find({
    timestamp: {$gt: new Date(+new Date() - 10* 60 * 1000)} //time in milliseconds
}).limit(10).toArray()

The above query will find 10 documents which are within 10 minutes(10 * 60 * 1000 milliseconds) from the current time . This is the command to execute in Mongo Shell.

If you are using Mongoose in Node.js you can use the following query as I 'am using it:

yourCollectionSchema.find({
      timestamp: {
        '$gt': new Date(+new Date() - 10 * 60  * 1000) //time in milliseconds
      }
    })
.then(function(docs){
    console.log(docs)// Use the data
});
Community
  • 1
  • 1
Nidhin David
  • 2,426
  • 3
  • 31
  • 45
  • thanks, but this give me the entries in minutes, i need to search in time periods, i want 10 entries separated by 10 minutes between them – MatCas Feb 29 '16 at 23:26