MongoDB组最小与条款
希望将结果与最低日期汇总在一起,但是最小应该仅适用于状态为真的字段?
鉴于以下数据,$ dateModified的$ min将返回日期为2,但是我希望返回3,因为它是最低的数据,并具有真实状态,
{name: "one", dateModified: "4", status: true},
{name: "one", dateModified: "2", status: false},
{name: "one", dateModified: "3", status: true}
.aggregate([
$group: {
_id: "$name",
date: {$min: "$dateModified" // where status is true},
total: {$sum: 1}
}
])
我还需要真实和错误条目的整体计数,因此请应用一个匹配状态:在小组之前是正确的
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不只是添加
$ match
阶段作为第一个使用状态过滤文档的阶段:true
?示例mongo playground
当您热衷于保持文档的
状态:false
状态:false ,然后您可以与$ cond> $ cond
和 。示例mongo Playground 2
Why not just add the
$match
stage as the first stage to filter the documents withstatus: true
only?Sample Mongo Playground
As you are keen to remain the document with
status: false
, then you can work with$cond
and$$REMOVE
.Sample Mongo Playground 2