I'm new to springboot and mongodb as well. I have the following json document in mongodb
Note: *Name of database is Friends and name of collection is Friend. It has around 118k documents.
One sample document:
[{
"_id":"abhj",
"id":"abhj",
"Main_array":[
{
"number":12345,
"pincode":247800,
"address": [
"vasant"
"vihar"
"kota"
]
}
],
}]
There is Main_array inside which there is an object inside which we have address which is an array.
I want to fetch the size of this address array.
I tried this but it didn't work.
Note: I have to use MongoClient.
MongoDatabase database = mongoClient.getDatabase("Friends");
MongoCollection<Document> collection = database.getCollection("Friend");
BasicDBObject filter = new BasicDBObject("Main_Array.0.address", new BasicDBObject("$exists", "true"));
collection.find(filter).forEach((Consumer<Document>) doc -> {
Object obj = doc.get("Main_array.address")
}
But I got null value in obj.