I'm very new to using Mongo and I have a collection that looks like this
{
"_id" : "5e7a39ed-941a-4e07-af0b-df8764820206",
"title" : "Test Title Task",
"taskBody" : "This is a test task body",
"comments" : [
{
"_id" : "57b51a73-f585-4e80-ad51-107da707efd6",
"poster" : "Jack Black",
"comment" : "This is a comment"
},
{
"_id" : "4ea314f3-3671-4568-b938-d8a1477ac681",
"poster" : "Joe Blow",
"comment" : "Another comment"
},
{
"_id" : "c5f1a0e6-2fb9-429e-9931-8634f42fc143",
"poster" : "Mike Hall",
"comment" : " And yet Another comment"
}
]
}
And I'm trying to get one of the comment elements by passing the ID like this:
getCommentById(id) {
return tasks().then((taskCollection) => {
return taskCollection.find({ "comments._id": id }).toArray().then((commentQuery) => {
if (!commentQuery) throw "Comment not found";
return commentQuery;
});
});
}
However, it seems to be returning everything in the comments collection and not just one comment that I'm passing in the id for. How can I get it to return just the comment that I'm passing the id for?
This is what the debug window shows me
Array(1) [Object]
Object {_id: "5e7a39ed-941a-4e07-af0b-df8764820206", title: "Test Title Task", taskBody: "This is a test task body", …}
Array(3) [Object, Object, Object, …]
0: Object {_id: "57b51a73-f585-4e80-ad51-107da707efd6", poster: "Jack Black", comment: "Comment text 2"}
1: Object {_id: "4ea314f3-3671-4568-b938-d8a1477ac681", poster: "Joe Blow", comment: "Another comment"}
2: Object {_id: "c5f1a0e6-2fb9-429e-9931-8634f42fc143", poster: "Joe Blow", comment: "Another comment"}
What I'm expecting to get returned is just:
{
"_id" : "57b51a73-f585-4e80-ad51-107da707efd6",
"poster" : "Jack Black",
"comment" : "This is a comment"
}
Assuming "57b51a73-f585-4e80-ad51-107da707efd6" is the passed in ID. Any help would be appreciated.