Im writing a simple query to return user content/posts. I also need to show the number of comments each post has.
My document structure looks like this
posts {
content: string,
comments: [array of object ids]
...
}
I know how to get the size of the comments array using aggregate function. I also know that i could add a "commentsNr" property and increment it using post save hooks. And...i know i can execute the query....return the posts array, and use the post.comments.length property, but i don't want to return the entire comments array just to count it.
All my queries are very simple, and i don't want to make them any more complicated than necessary. So, im looking for a way to return the length of the comments array as a new field, i.e use projection or something like that.
Is there a way to use simple find query to get the length of the document array, without returning the array itself ?