Java Date in MongoDB aggregation query brings no result. Collection has data and unit test works

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<>();
    .forEach(bsonValue -> queryPipeline



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.

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)}}} 

Hi @Raj and welcome in the MongoDB Community :muscle: !

I wrote a little example. I hope this will help:

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.json.JsonWriterSettings;

import java.time.Instant;
import java.util.Date;
import java.util.function.Consumer;

import static com.mongodb.client.model.Filters.gte;

public class Community {

    public static void main(String[] args) {
        try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
            MongoDatabase db = mongoClient.getDatabase("test");
            MongoCollection<Document> coll = db.getCollection("coll");
            coll.insertOne(new Document("date", new Date(988307200000L)));

            System.out.println("It works like this with a timestamp and Date:");
            coll.find(gte("date", new Date(978307200000L))).forEach(printDocuments());

            System.out.println("It's also working with Instant and Date for example:");
            Instant instant = Instant.parse("2000-01-01T00:00:00.000Z");
            Date timestamp = Date.from(instant);
            coll.find(gte("date", timestamp)).forEach(printDocuments());

    private static Consumer<Document> printDocuments() {
        return doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build()));


1 Like

Hi, I am also having the same issue, but I am trying this using Spring MongoTemplate library. Anyone has a Spring solution for this?