Sensei
Sensei

Reputation: 173

Running prepared mongodb queries using Java driver

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 finds 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 ?

Upvotes: 2

Views: 324

Answers (2)

mtj
mtj

Reputation: 3554

It seems that the new Date syntax is not supported in the query, but only in the internal Javascript libraries.

For the query, replace "new Date" with "ISODate" (e.g. ISODate("2017-01-10T10:13:58.630Z") ) and it should be working.

Upvotes: 0

s7vr
s7vr

Reputation: 75934

You'll need to update query template to a compatible JSON type so it can be parsed.

Query Template Json:

{
    "_id": ObjectId("SomeId#3"),
    "_class": "com.example.dao.entity.Query",
    "query": "{"runDate":{ "$gte" :{"date" : { "$date" : "%1$s"}}}}"
}

Code:

Instant dateTime = Instant.now();
String dateRange = String.format(getDateRangeQuery(),dateTime.toString()));

Now you can run this as a regular query after JSON.parse().

More here on other types:

https://docs.mongodb.com/manual/reference/mongodb-extended-json/

Upvotes: 1

Related Questions