Cauca
Cauca

Reputation: 402

substract two date end return value

I need help to build a query to substract two dates in mongodb.

I have some documents like above :

{"_id" : "32472034809", "center": "102030", dateArq : 141010, inDate : "ISODate("2014-06-06T02:57:19.000-03:00)", biDate : ISODate("2014-06-07T02:57:19.000-03:00)"}

And Im trying to write a query

db.teste.aggregation([{$match : {dateArq : 141010}},{$project : {$subtract : ["$biDate" "$inDate"]}}])

In fact, I want to do : for each _id I want to result biDate - inDate , because I need to see if dateArq keep in a line constante.

In Oracle I did

select dateArq, (biDate - inDate) diff from teste where dateArq = 141010

Tks for help

Upvotes: 0

Views: 821

Answers (1)

wdberkeley
wdberkeley

Reputation: 11671

The document and aggregation pipeline provided had syntax problems, and you needed to put a field name for the result of the $subtract, but otherwise your pipeline works for me:

> db.test.findOne()
{
    "_id" : "32472034809",
    "center" : "102030",
    "dateArq" : 141010,
    "inDate" : ISODate("2014-06-06T05:57:19Z"),
    "biDate" : ISODate("2014-06-07T05:57:19Z")
}
> db.test.findOnedb.test.aggregate([
    { "$match" : { "dateArq" : 141010 } }, 
    { "$project" : { "dateDiff" : { "$subtract" : ["$biDate", "$inDate"] } } }
])
{ "_id" : "32472034809", "dateDiff" : NumberLong(86400000) }

Upvotes: 1

Related Questions