I have a mongodb collection with the following fields:
_id
name
(string) [items]
(array of string) secret
(boolean) 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.
I have read somewhere that I can add additional document properties in my find query, but my google foo does not work.
Here is what my publish method looks like without the additional items_count
property:
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?
EDIT : it seems that I need to use an aggregate
function and the $project
operator. 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:
myItems.aggregate(
[
{$match: {secret: true}},
{$project: {_id: 1, name: 1, items_count: {$size: '$items'}}}
]
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.