我有一个文件:
{
_id: 98556a665626a95a655a,
first_name: 'Panda',
last_name: 'Panda,
notifications: [{
_id: '',
// ...
}]
}
我想返回如下回应:
{
_id: 98556a665626a95a655a,
first_name: 'Panda',
last_name: 'Panda',
notifications: 2
}
的问题是有关通知计数字段,
我用猫鼬NodeJS软件包,我试过以下内容:
UserDBModel.findOne({_id: uid}, {notifications: {$size: '$notifications'}}, function(err, user){ });
但它似乎不起作用。 有人可以帮助我吗? :)
在此先感谢。
该查询返回给您什么? – CodePhobia
$ size操作符只会返回与您使用时指定的大小相匹配的字段(它需要一个数字)。如果你想要它*返回数组中的项目数*然后你必须在**聚合**中使用它。这里是[链接](https://docs.mongodb.org/v3.0/reference/operator/aggregation/size/)。如果不是,请尝试使用数组的.length属性,如答案中所示。 – jmugz3