Reputation: 3043
I have a question and comment section in my page and comments has facebook like vote option
i want to sort the comments based on the length of the array
this is my document
{
"_id" : "Xxxx",
"comments" : [
{
"cmt_text" : "op1",
"votes" : [
"Sasikanth",
"Sasikanth"
]
},
{
"cmt_text" : "op2",
"votes" : [
"Sasikanth",
"Sasikanth",
"Sasikanth"
]
},
{
"cmt_text" : "op3",
"votes" : [
"Sasikanth"
]
}
],
"question" : "test question"
}
i want the comments based on the length of the votes.
Upvotes: 1
Views: 725
Reputation: 5472
You have a number of options:
You can use transform to also get the array length as an additional document property. But that won't be reactive.
You can hook into observeChanges to get your count (which is documented with an example on the official docs), but that will be hard to maintain.
You can use a third party package like collection2 to publish virtual fields that are computed from other info (ie length of the array)
You can take the length of the array within your template helper, but you'd need to rewind the data if you go through all comments
Just have a voteCount property and $inc int when you $push votes to the array.
I'd go for 5. In fact, I am currently working on a very similar functionality on a project and that's how I do it.
Upvotes: 2