MongoDB Client query with ISODate

1,735 views
Skip to first unread message

Filipe Delgado

unread,
May 10, 2016, 4:29:49 AM5/10/16
to vert.x
Hi guys,
can someone tell me how to perform a query with ISODate with MongoVertx Client??

An Example (query in mongo):
{
"aggregate": "collectionA",
"pipeline": [{
"$match": {
"creation_date": {
"$gte": ISODate("2016-05-09T06:48:11.526+01:00")
}
},
"$lookup": {
"from": "collectionB",
"localField": "a_id",
"foreignField": "_id",
"as": "b"
}
}]
}

Richard MENG

unread,
May 10, 2016, 5:00:43 AM5/10/16
to vert.x
You should use the JSON object to make a ISODate, e.g. new JsonObject().put("$date", Instant.now().toString())

This is MongoDB's JSON extension, check it here @ https://docs.mongodb.com/manual/reference/mongodb-extended-json/


Thanks,

Richard-

Filipe Delgado

unread,
May 10, 2016, 5:28:03 AM5/10/16
to vert.x
Hi Richard.
I've already tried this approach.It works when writing/reading but in filter it doesn't.

This works:
"creation_date": {
"$gte": ISODate("2016-05-09T06:48:11.526+01:00")
}

But this doesn't:

"creation_date": {
"$gte": {"$date" : "2016-05-09T06:48:11.526+01:00"}

Richard MENG

unread,
May 10, 2016, 9:32:45 AM5/10/16
to vert.x
Probably you should do a debug into MongoClient to see what is converted. Then open a bug against MongoClient.

Filipe Delgado

unread,
May 10, 2016, 10:13:46 AM5/10/16
to vert.x
I think the problem is that mongoclient only supports JsonObject and, in a JsonOBject, the ISODate part is always escaped, otherwise is not a valid Json.

"$lte": "ISODate(\"xxxxx\")" - this is a valid JSON but not a ISODate operator
 "$lte": ISODate("xxxxx") - this is a invalid JSON but valid ISODate operator

Paulo Lopes

unread,
May 10, 2016, 3:20:53 PM5/10/16
to vert.x
The dates should be in Zulu timezone, e.g.: 2015-05-30T22:50:02Z that is the default from RFC-7493 the json extension for temporal types. You're using +01:00

Filipe Delgado

unread,
May 11, 2016, 4:31:32 AM5/11/16
to vert.x
Finally found the solution: https://github.com/vert-x3/issues/issues/118
"creation_date": {
"$gte": {
"$date": "2016-05-01T11:13:27.519Z"
}
If you use Joda DateTime:
  1. parse datetime with timezone (if need, in my case this comes from a  REST API in string) - DateTime creationDate = ISODateTimeFormat.dateTime().parseDateTime(filter.getCreationDateFrom()).toDateTime(DateTimeZone.UTC)
  2. create query JsonObject - new JsonObject().put("$gte",new JsonObject().put("$date", ISODateTimeFormat.dateTime().print(creationDate))
Reply all
Reply to author
Forward
0 new messages