I have a chats
collection witch sample document
{
"_id" : ObjectId("57f95e8e385bb61c5cf2cd18"),
//some other fields
"messages" : [
{
"sender" : "57ec1aaa0ffe16123439d52b",
"message" : "Hello!",
"sentTime" : "Oct 13, 2016 1:51:31 AM"
},
{
"sender" : "57ec1aaa0ffe16123439d52b",
"message" : "Hello!",
"sentTime" : "Oct 13, 2016 1:51:33 AM"
}
]
}
I want to search the collection on the basis of _id
field and only get the data of the messages sub-document
as the query result using java driver for MongoDB as I want to Map to POJO array using Gson.
Desired Output:
[
{
"sender" : "57ec1aaa0ffe16123439d52b",
"message" : "Hello!",
"sentTime" : "Oct 13, 2016 1:51:31 AM"
},
{
"sender" : "57ec1aaa0ffe16123439d52b",
"message" : "Hello!",
"sentTime" : "Oct 13, 2016 1:51:33 AM"
}
]
How do I go with this using Java driver for MongoDB