[英]How to publish a mongodb array length as an additional collection field?
I have a mongodb collection with the following fields: 我有一个mongodb集合,其中包含以下字段:
_id
name
(string) name
(字符串) [items]
(array of string) [items]
(字符串数组) secret
(boolean) secret
(布尔值) I want to publish the all the _id, name fields and the item array length only (excluding the secret field) where the secret field is true. 我只想发布所有_id,名称字段和项目数组长度(不包括秘密字段),其中秘密字段为true。
I have read somewhere that I can add additional document properties in my find query, but my google foo does not work. 我读过某处可以在查找查询中添加其他文档属性的信息,但我的google foo无法正常工作。
Here is what my publish method looks like without the additional items_count
property: 没有其他
items_count
属性的情况下 ,我的发布方法如下items_count
:
Meteor.publish("all_items", function() {
return myItems.find(
{secret: true},
{fields:
{_id:1,name:1}
});
});
How can I create an additional field from the [item]
length in my publication? 如何从出版物的
[item]
长度创建附加字段?
EDIT : it seems that I need to use an aggregate
function and the $project
operator. 编辑 :似乎我需要使用
aggregate
函数和$project
运算符。 And it is not supported by meteor. 流星不支持它。
Can anyone confirm this to me (ie it is the only option and it is not supported)? 谁能向我确认这一点(即这是唯一的选择,并且不受支持)?
You can add aggregation framework
support to Meteor
, and then use a simple aggregation pipeline with $project
stage as you mentioned, like to following: 您可以向
Meteor
添加aggregation framework
支持 ,然后使用具有$project
阶段的简单聚合管道,如下所述:
myItems.aggregate(
[
{$match: {secret: true}},
{$project: {_id: 1, name: 1, items_count: {$size: '$items'}}}
]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.