node.js - 获取属性猫鼬的数组长度
问题描述
在我的情况下,我需要属性“idsProfil”中的 id 数量。我不希望属性中的所有 id ,只是数字。
这就是我获得出版物的方式。
const publication = await this.publicationService.paginated(
{
$and: [
{ deletedAt: null },
],
$or: [
{ profile: friends},
{ profile: req.user.profile},
]
},
{populate: [
['profile'],
['like'],
]},
req.query)
这就是我的对象的创建方式。
const LikeSchema = new Schema({
_id: String,
idsProfil: {
type: [String],
}
})
我不能使用像月光一样的预验证,因为我使用 FindAndUpdate,我不能使用 Save()
解决方案
要使用 Mongoose 获取集合中存在的文档数量,您可以使用Model.estimatedDocumentCount()。它将使用元数据浏览您的收藏。
另一种方法是使用Model.countDocuments()。但是,由于它浏览集合的方式,此功能在大型集合上效率不高。它正在扫描整个集合并计算每个文档,效率不高。
推荐阅读
- bash - 不推荐使用没有加载程序的 yaml.load(),--backend-config
- python - 如何将用逗号分隔的单个 BYTE 元素转换为列表的更多元素
- javascript - 仅在 Kendo 网格中为一列启用列菜单,并在页面加载时默认隐藏特定列
- ruby - 在 Ruby 中,array.select { |x| 是什么?array.count(x) > 1 } 的意思是什么?
- filter - 隐藏搜索过滤器的 JQL 查询
- c# - 如何访问作为 DependencyProperty 公开的 WPF UserControl 的子控件?
- javascript - 反应测试无法找到用玩笑嘲笑的元素
- java - 后映射期间的验证不会调用 ConstraintValidator 的 isValid 函数
- javascript - 函数参数有问题我不能引用这个函数
- reactjs - 如何合并自定义 Hook 提供程序但保持它们的用例分开