I tried to reproduce the issue on my local setup with MongoDB version 5.0.7
Using the data you posted, the following query proved to be useful in finding the size of the address array.
MongoDB Compass provides the feature to export aggregation pipeline to specific language. Please refer to the documentation export-pipeline-to-language
Below is the Java code for the above aggregation query.
/*
* Requires the MongoDB Java Driver.
* https://mongodb.github.io/mongo-java-driver
*/
MongoClient mongoClient = new MongoClient(
new MongoClientURI(
"mongodb://localhost:27018/"
)
);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("example");
FindIterable<Document> result = collection.aggregate(Arrays.asList(new Document("$unwind",
new Document("path", "$Main_array")),
new Document("$addFields",
new Document("Main_array.address_size",
new Document("$size", "$Main_array.address")))));
Please note that if the Main_array contains more elements in the array, the above aggregation would need a projection stage in the pipeline to suit your specific need.