node.js - 查找数组大小等于字段的位置
问题描述
我正在尝试查找数组大小等于字段值的所有文档。例如:
应该找到该文件:
{
arr: ["one", "two", "three"],
expected: 3
}
但不是这个:
{
arr: ["one", "two", "three", "four"],
expected: 2
}
我假设我必须使用某种形式的聚合,所以我决定使用$expr
:
$expr: { $eq: [{ $size: "$arr" }, { ??? }] }
解决方案
arr
与您使用符号使用字段类似$
,您必须使用expected
字段
db.collection.find({ "$expr": { "$eq": [{ "$size": "$arr" }, "$expected"] })
推荐阅读
- java - 主线程和单个长时间运行线程之间的线程安全双向通信
- c# - 使用导航属性更新实体正在尝试为属性实体 Entity Framework Core 2 插入新行
- c# - 使用 MVC Web 应用程序将图像文件上传到远程服务器中的文件管理器
- macos - 为什么 WebStorm 文件对话框是灰色和反转的?(在 MacOS 上发生,在安装 Elm 之后)
- java - 尝试使用 akka 流编组 jaxb 对象时出现空文件
- javascript - 用另一个数组中的元素替换数组中的元素
- privileges - 在 Windows 操作系统上将应用程序作为服务运行和作为管理员运行有什么区别?
- .net - 某些 Enum E 的 E 和 E^ 有什么区别?(C++/CLI)
- powershell - 使用 powershell 挤压空行
- android - 我可以在 webview 中打开本地 html 链接吗?