Query to catch date difference

Asked

Viewed 115 times

0

I wonder how I can make a query of this type in Mongodb with date difference and picking the last record in a date range.

SELECT *, DATEDIFF(NOW(),DATAHORA) AS DIAS FROM TBLTRANSFERS 
        WHERE IDTRANSFER IN (SELECT MAX(IDTRANSFER) 
                       FROM TBLTRANSFERS WHERE DATAHORA BETWEEN '2017-10-26 11:42:28' 
                       AND '2017-11-13 11:42:28' 
                       AND (EVENTO = 1 OR EVENTO = 3) 
                       AND RESULTADO = 0 GROUP BY USUARIO) 
ORDER BY USUARIO DESC;

1 answer

0

An example of taking data between a date range would be:

Let’s assume that Voce saves like this:

collection.save({
    name: "example",
    dataCriacao: "Sun May 30 18.49:00 +0000 2010"
})

You could consult:

collection.find({"dataCriacao":{$gte:new ISODate("2017-11-20T23:59:59Z"),$lte:new ISODate("2017-11-21T11:46:59Z")}}).count().pretty();

Or else:

collection.find({
    dataCriacao: {
        $gte:"Mon May 30 18:47:00 +0000 2015",
        $lt: "Sun May 30 20:40:36 +0000 2010"
    }
})

Check: Sometimes it is necessary to convert to the date format that Mongodb supports, so the example ISODate.

See more examples in the Mongodb.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.