I want to retrieve an array from MongoDB using Spring Data Mongo.
I have collection consists of users with todos:
{
"_id" : ObjectId("584d863e439e512f4c457eea"),
"username" : "e",
"todos" : [
{
"_id" : ObjectId("584d8654439e512f4c457eeb"),
"title" : "Officiis id sed aut molestiae id id magni.",
"body" : "Fugit velit rem incidunt. Et ad aliquam inventore voluptatem sed omnis voluptas. Aut sint eum voluptatem.",
"when" : ISODate("1972-09-15T04:24:08.880Z"),
"priority" : "HIGH",
"status" : "DEFAULT",
"tags" : [
"animi",
"veniam",
"exercitationem"
]
},
// ...
]
}
I have tried the following:
public List<Todo> getUserTodos(String userId)
Query x = new Query(Criteria.where("_id").is(userId));
x.fields().exclude("_id").include("todos");
return mongoTemplate.find(x, Todo.class);
}
Actually, I am getting an empty ArrayList. The response in mongo cli is like:
{
"todos": [ {/* todo1 */}, {/* todo2 */} ]
}
How to return array from MongoDB or how to unwrap it for deserializing in Java?
_idin the query is different from what shown in the sample.userIdparameter there. I will fix question.