在meteor中使用Mongo聚合
我有我的collections如下:
id: 'some_id' items: [ {name:x}, {name:y} ]
当我做collection_object.find({id:'some_id'},{items:1,_id:0})
,它返回一个对象,而不是实际的数组。 然后我跟着这里的答案 ,并使用我的meteor帮手中的aggregate
函数,而不是查找。 新的呼叫是:
collection_object.aggregate({$match:{id:'mseb'}}, {$unwind:'$items'} )
这提出了aggregate
未定义的错误。
两个问题:
1.如果find
调用返回一个包含数组的对象,那么如何访问模板中数组的单个元素。 使用{{this.items}}
再次显示一个数组,“对象”和{{this.items.name}}
是未定义的。
2.如果不是1,那么怎样才能和meteor合作。
更新:我遵循meteorhacks:聚合的方法,但也给出了相同的结果。 下面的答案为我工作。
- 你可以做:
{{#each this.array}} {{name}} {{/each}
提示:你也可以做collection.find().fetch()
它会以Array的forms返回所有匹配的文档。 http://docs.meteor.com/#/full/fetch
- 聚合不支持meteor,你可以添加
meteor add meteorhacks:aggregate