I am having hard time understanding the issue i am face with making the aggregation pipeline query work. I read the query as string from a file and then convert that into Bson and then input that to aggregation() before executing it.
Below is the code
String query = ResourceLoader.loadResource("mongodb/search_query.txt");
List<Bson> queryPipeline = new ArrayList<>();
BsonArray.parse(query)
.getValues()
.forEach(bsonValue -> queryPipeline
.add(BsonDocument.parse(bsonValue.toString())));
collection.aggregate(queryPipeline);
Issue:
Below is the matching condition that is defined in the file
{ $match: {
"modified":{$gte: new Date(978307200000)}
}
}
the above string gets parsed into the below Bson object
{"$match": {"modified": {"$gte": {"$date": "2001-01-01T00:00:00Z"}}}}
Problem is that, the query doesn’t return any result from the collection. even more confusing is that the unit test I wrote passes, meaning; the same query fetch the record and the test passes.
Below is a sample record that is stored in our collection. I see modified
is stored as Date.
_id:62b9e49c772160a59079ae5d
myId:"26085"
source:"has some data:
modified: 1999-07-05T00:00:00.000+00:00
I am not sure what the issue is, I would really appreciate your help in understanding the issue.
if I run the query on the mongo shell setting new Date(), it works; I understand MongoShell doesn’t understand $date but Bson parser outputs the data condition as $date:
{"$match": {"modified": {"$gte": new Date(978307200000)}}}