fab*_*ien 2 javascript collections mongodb meteor
我有一个简单的出版物:
return Companies.find({}, {fields: {'myField1': 1, 'myField2': 1}});
在我的Companies收藏中,对于每个公司,我都有一个数组customers和一个数组managers.这些数组包含具有'_id'和各种其他属性的对象.
对于可视化,可以添加新公司,如下所示:
Companies.insert({
customers: [{_id: <userId>, otherProp: <data>}, ...],
managers: [{_id: <userId>, otherProp: <data>}, ...]
});
Run Code Online (Sandbox Code Playgroud)
该_id字段是users集合中相应用户的id.
我想只返回那些可以在customers数组的对象(或管理器数组,具体取决于用户)中找到用户的_id的公司.
这可能是一个mongo问题,但我不确定.=>在文档中,他们提到了mongo选择器:http://docs.meteor.com/#selectors ( http://docs.mongodb.org/manual/reference/operator/query/)但我无法弄清楚如何把它用于我的情况.
我觉得你可以这样做:
Companies.find({'customers._id': this.userId}, {fields: {'stores': 1, 'customers': 1}});
这将在数组内搜索.
但是现在,如何完成过滤器并仅返回客户的信息,而不是返回公司所有客户的信息?
答案再次出现在mongo doc中!
我似乎可以在字段上添加投影.(http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch)
使用适当过滤器的最终请求是:
return Companies.find({'customers._id': this.userId}, {fields: {'stores': 1, 'customers': { $elemMatch: { _id: this.userId } }}});
(最后我没那么努力,但如果我的回答可以帮助某人...... :))
| 归档时间: |
|
| 查看次数: |
1792 次 |
| 最近记录: |