Meteor 对文档中的数组进行排序和切片
我的医生: 我想从这个文件发送最近的3条评论,怎么做 我尝试了不同的方法,但没有效果Meteor 对文档中的数组进行排序和切片,meteor,Meteor,我的医生: 我想从这个文件发送最近的3条评论,怎么做 我尝试了不同的方法,但没有效果 "_id" : "bf63XML4bo6CrfP9A", "comments" : [ { "user" : "fzkhiAArD4mgAAjbL", "comment" : "what the hell are you doing in there?", "commentedAt" : 1422367883366
"_id" : "bf63XML4bo6CrfP9A",
"comments" : [
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "what the hell are you doing in there?",
"commentedAt" : 1422367883366
},
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "tada tada",
"commentedAt" : 1422368379037
},
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "la la la la la",
"commentedAt" : 1422368393536
},
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "no no no",
"commentedAt" : 1422368409692
}
],
"feed_id" : "ym8To4cdwwTcSczkS",
FYO
我想在“发布”功能中使用它。据我所知,您不能对评论进行排序,然后对它们进行切片,只能对它们进行切片。如果您真的需要排序和切片,您可能应该使用a,尽管我认为这在计算上会更昂贵 但是,如果您已按时间顺序将$PUSH注释推送到数组中,您应该能够(仅在服务器上)执行以下操作: 去拿最后三个
mycoll.find({_id:res._id},{fields:{sort: {commentedAt: 1}},fields: {comments:{$slice: 3}}});
mycoll.find({_id:res._id},{fields: {comments:{$slice: -3}}});