I'm adapting the Microscope tutorial to my needs and am having difficulty writing a query which gathers all the comments embedded within a post.
Here is my query so far:
Posts.find({"postId": this._id}, {"comments":{}});
Here is an example post I want to get the comments from:
{
"_id": "Ad9RYqWqbsJKZx3h7",
"title": "Writing decimal number words as a decimal number",
"userId": "9yqTaFeQSqvKmNn8B",
"author": "Sacha Greif",
"submitted": "2017-01-05T03:26:18.908Z",
"commentsCount": 4,
"comments": [
{
"body": "Hello",
"postId": "Ad9RYqWqbsJKZx3h7",
"userId": "73qGvsRuqNtXcaZDx",
"author": "student",
"submitted": "2017-01-05T10:26:45.745Z"
},
{
"body": "How are you?",
"postId": "Ad9RYqWqbsJKZx3h7",
"userId": "73qGvsRuqNtXcaZDx",
"author": "student",
"submitted": "2017-01-05T10:28:17.225Z"
}
]}
It seems to return a cursor, but I am not able to use the toArray()
function on it.
I have read this is not possible (Filtering embedded documents in MongoDB), but this was six years ago...
I've also seen posts about $slice
and aggregate
but can't seem to get me head around them.
Any help much appreciated!