Filter data of a collection by date

1

I need to filter the occurrences of the last hour of the next mongodb query;

db.OPERATIONS.find({"Description":"Not playable."}).count()

Thank you very much in advance.

Greetings.

    
asked by Kleiner zin 23.10.2018 в 11:08
source

2 answers

0

assuming you do not have the fecha field in the Collection, I'll tell you that with the ObjectId you can have the date that the application was registered you can see it in the following way:

ObjectId.getTimestamp()

Once this has been clarified, the next thing is to look for the records of the last hour ( one hour apart from the current one ); you must calculate the last hour and compare it with the timestamp of the ObjectId () , the difference is that we can not do it directly, we must use the function $ where to be able to calculate the date

You can make the following query:

db.OPERATIONS.find({ 
    $where: 
    function() { 
        var ultimaHora = new Date();
        ultimaHora.setHours(ultimaHora.getHours() - 1);
        return this.Description ==  "Not playable" &&   this._id.getTimestamp() >= ultimaHora 
        } 
    }).count()
    
answered by 23.10.2018 / 15:29
source
0

See if I understood well you want to get the docs of Description: not playable of the last hour you can use the operators $ and, $ eq and $ gte .

  • the $ and specify that both fields must be valid on
  • $ eq that the description field must be equal to "Not Playable"
  • and the $ gte the date is greater than the entered date

$ gte es > = you can also use $ gt which is >

db.operations.find(
  {$and: [ 
     { "Description": { $eq:"Not playable"  } }, 
     { fecha: { $gte: "La fecha" } } 
  ] } 
)

maybe the query is not ordered exactly because I do not know how to handle the date but those operators should help you if or if

    
answered by 23.10.2018 в 14:36