Query to get date difference

0

I would like to know how I can make a query of this type in MongoDB with date difference and taking 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;
    
asked by anonymous 14.11.2017 / 15:41

1 answer

0

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

Let's say you save it 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 also:

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 ISODate example.

See more examples in the official MongoDB documentation.

    
21.11.2017 / 14:52