MongoDB with Java driver pipeline match date not working

Hi,

I have basic operation where I’m doing aggregation with date data in MongoDB with Java.
I’m not getting proper output in java but when I’m executing it in mongo shell it works fine.

{
    "_id": {
        "$oid": "64a67f32dbe7c36e2e6c15c8"
    },
    "name": {
        "user": "xyz"
    },
	"updated_at": {
		"$date": "2023-07-20T22:44:51.334Z"
	}		
    "data": {
        "age": "55",
        "address": [{
                "$ref": "Addresses",
                "$id": {
                    "$oid": "64a67f2fdbe7c36e2e6c15c6"
                }
            }, {
                "$ref": "Addresses",
                "$id": {
                    "$oid": "64a67f2fdbe7c36e2e6c15c7"
                }
            },
        ]
    }
}

Addresses:

{
  "_id": {
    "$oid": "64a67f2fdbe7c36e2e6c15c6"
  },
  "name": {
    "type": "permenant"
  },
  "address":"permenant address 1"
}

{
  "_id": {
    "$oid": "64a67f2fdbe7c36e2e6c15c7"
  },
  "name": {
    "type": "secondary"
  },
  "address":"permenant address 2"
}

Java code:

private static void getData(MongoCollection<Document> products) {
		Date currentDate = new Date(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5));
		Bson match = match(gte("updated_at", currentDate));
		Bson lookup = lookup("address", "address.$id", "_id", "address");
		Bson unwind = unwind("$address");
		List<Document> results = products.aggregate(Arrays.asList(match, lookup, unwind)).into(new ArrayList<>());
		System.out.println("result given" + results.size());
		results.forEach(printDocuments());
	}

Hi @Janak_Rana and welcome to MongoDB community forums!!

Based on the sample documents provided, I tried to replicate the code in my local environment and I was successful to get the expected output.
I tried the code below:

package com.company;

import com.mongodb.MongoClientSettings;
import com.mongodb.client.*;
import org.bson.Document;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

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


public class QuickTour {

    public static void main(final String[] args) {

        String mongoURI = "mongodb+srv://findThief:findThief@cluster0.sqm88.mongodb.net/";
        MongoClient mongoClient = MongoClients.create(mongoURI);
        CodecRegistry codecRegistry = MongoClientSettings.getDefaultCodecRegistry();

        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection<Document> collection = database.getCollection("users");

        System.out.println("successful");
        final DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap());
        Date currentDate = new Date(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5));         
        Bson match = match(gte("updated_at", currentDate));         
        Bson lookup = lookup("Addresses", "data.address.$id", "_id", "address");         
        Bson unwind = unwind("$address");        
        List<Document> results = collection.aggregate(Arrays.asList(match,lookup,unwind)).into(new ArrayList<>());         //System.out.println("result given" + results.size());
        for (Document document : results) {
            System.out.println(document.toJson(codec));
        }

        mongoClient.close();
    }
}

The aggregation pipeline given by you has an issue with the lookup stage.
After the $match operation, the output would look like:

{
  "_id": {
    "$oid": "64a67f32dbe7c36e2e6c15c8"
  },
  "name": {
    "user": "xyz"
  },
  "updated_at": {
    "$date": 1690757091334
  },
  "data": {
    "age": "55",
    "address": [
      {
        "$ref": "Addresses",
        "$id": {
          "$oid": "64a67f2fdbe7c36e2e6c15c6"
        }
      },
      {
        "$ref": "Addresses",
        "$id": {
          "$oid": "64a67f2fdbe7c36e2e6c15c7"
        }
      }
    ]
  }
}

The next stage as $lookup, uses the localField as address.$id which should be data.address.$id to perform the lookup with _id of the Addresses collection.
In your code, after the match stage, the lookup was resulting into an empty address array which further could not unwind the empty array and was not providing the correct output as expected.

Could you please confirm, if the above code works for you?

P.S: The code above is provided using the latest 4.10 MongoDB Java driver version.

Let us know if you have any further questions.

Regards
Aasawari

Sorry for late reply and I have tested your code but didn’t worked for me.
Below is the date data in my mongo db.
“updated_at”: {
“$date”: “2023-08-07T18:53:43.336Z”
},