In the MongoDB (v3.2.8) I have some bucketed values stored in a time sequence. I'm currently trying to aggregate data out of these buckets using the Morphia framework (v1.1.0), limiting the documents by the date of the document (a field, see sample below).
{
"_id" : ObjectId("57b696548376400e6e56a18a"),
"date" : ISODate("2016-08-19T00:00:00.000Z"),
"kpiId" : "1.2",
"history" : [
{
"name" : "02. Chilled Water Temperature",
"timestamp" : ISODate("2016-08-19T05:28:29.343Z"),
"value" : "6"
},
{
"name" : "02. Chilled Water Temperature",
"timestamp" : ISODate("2016-08-19T05:28:54.721Z"),
"value" : "1"
},
{
"name" : "02. Chilled Water Temperature",
"timestamp" : ISODate("2016-08-19T05:30:31.003Z"),
"value" : "21"
},
{
"name" : "02. Chilled Water Temperature",
"timestamp" : ISODate("2016-08-19T05:31:58.458Z"),
"value" : "20"
}
],
"asset" : {
"id" : "1",
"name" : "LTD121",
"contract" : {
"id" : "MyCompany",
"name" : "MyCompany"
}
},
"count" : 4
}
For background information I'm using the Morphia framework to generate the queries to MongoDB. When I aggregate with Morphia using the date and kpiId
and asset.contract.id
generates the following match query:
{ "$match" : {
"asset.contract.id" : "MyCompany" ,
"kpiId" : "1.2",
"date" : { "$gte" : { "$date" : "2016-08-19T00:00:00.000Z"}}
}}
This query however does not return any documents in the collection, when I expected the document in the sample to be returned. It gets even stranger when I manually change the query to the query below I do get the document from MongoDB.
{ "$match" : {
"asset.contract.id" : "MyCompany",
"kpiId" : "1.2" ,
"date" : { "$gte" : ISODate("2016-08-19T00:00:00.000Z")}
}}
Why is the first query not working, and if it is an invalid or incorrect query how do I manipulate Morphia to create the query correctly.
Update: per request adding the related Java code that we use to instruct morphia to create the query:
datastore.createAggregation(HistoryBucket.class)
.match(datastore.createQuery(HistoryBucket.class)
.field("asset.contract.id").equal(contractId)
.field("kpiId").equal(kpiId)
.field("date")
.greaterThanOrEq(CalendarUtils.truncateToDayUTC(startDate)))
.aggregate(HistoryBucket.class);
In this the startDate
is of type java.util.Date
and the return of the call to CalendarUtils.truncateToDayUTC
also returns a java.util.Date
with the minutes, hours and seconds set to 0.