I am having trouble formatting the query below for it to be parse-able by the JSON utility provided by MongoDB Java driver and I'd like suggestions. First, some context.
A test framework, at the end of every run, stores the test results in MongoDB. Here are 2 such samples from collection 'TestResults':
{
"_id" : ObjectId("SomeId#1"),
"_class" : "com.example.dao.entity.TestResults",
"testScenario" : "Test login",
"runId" : 314,
"runDate" : ISODate("2016-12-14T15:51:04.586Z"),
"status" : "passed"
}
{
"_id" : ObjectId("SomeId#2"),
"_class" : "com.example.dao.entity.TestResults",
"testScenario" : "Test reset password",
"runId" : 314,
"runDate" : ISODate("2016-12-14T20:50:01.269Z"),
"status" : "passed"
}
And, another collection called 'Queries' contains pre-written query templates. The query below find
s Documents by runDate
:
{
"_id": ObjectId("SomeId#3"),
"_class": "com.example.dao.entity.Query",
"query": "{\"runDate\":{\"$gte\":new Date(\"%1$s\"),\"$lt\":new Date(\"%2$s\")}}"
}
The intent is to use the above query template, after adding a valid time range, on the aforementioned 'testResults' collection and process further. Here's what I've tried so far:
Using JSON.parse():
LocalDateTime dateTime1=LocalDateTime.of(2016, 12, 14, 00, 00,00);
LocalDateTime dateTime2 = dateTime1.plusHours(24);
/* Gets date range query from 'Queries' collection */
String dateRange = getDateRangeQuery();
/* Apply time range for last 24 hours... */
dateRange = String.format(dateRange,dateTime1.format(DateTimeFormatter.ISO_DATE_TIME),dateTime2.format(DateTimeFormatter.ISO_DATE_TIME));
BasicDBObject dayQuery=(BasicDBObject) JSON.parse(dateRange);
FindIterable<Document> result = database.getCollection("testResults").find(dayQuery);
result.forEach(new Block<Document>() {
@Override
public void apply(Document t) {
System.out.println("QUERY RESULT:"+t.toJson());
}
});
On running, JSON.parse() throws this exception
com.mongodb.util.JSONParseException:
{"runDate":{"$gte":new Date("2016-12-14T00:00:00"),"$lt":new Date("2016-12-15T00:00:00")}}
^
at com.mongodb.util.JSONParser.read(JSON.java:301)
at com.mongodb.util.JSONParser.parse(JSON.java:172)
at com.mongodb.util.JSONParser.parseObject(JSON.java:263)
at com.mongodb.util.JSONParser.parse(JSON.java:227)
at com.mongodb.util.JSONParser.parseObject(JSON.java:263)
at com.mongodb.util.JSONParser.parse(JSON.java:227)
at com.mongodb.util.JSONParser.parse(JSON.java:155)
at com.mongodb.util.JSON.parse(JSON.java:92)
at com.mongodb.util.JSON.parse(JSON.java:73)
Using BsonDocument.parse():
BsonDocument.parse()
was able to parse the query but I couldn't find a way to configure the collection name for the BsonDocument
to be then used in MongoDatabase.runCommand()
So now what are my options ?