mongodb - 如果字段存在必须为真,但如果不存在则必须像真一样通过
问题描述
我想检查 pipeline aggregate
( existeTransformacion
) 中是否存在字段。如果此字段存在必须为真才能通过($match
),如果为假,我需要从我的结果中排除,但如果不存在则必须通过。我怎样才能做到这一点?
{
//...more data
"ubicacionActual": {
"transformacion": {
"trabajando": true,
}
},
//This field come from $project in this way
//$project: {existeTransformacion: '$ubicacionActual.transformacion.trabajando'}
"existeTransformacion": true,
"paso": 1
},
所以基本上:
如果存在
existeTransformacion
并且existeTransformacion===true
必须显示。如果存在
existeTransformacion
并且existeTransformacion===false
必须不显示。- 如果不存在必须显示。
解决方案
您需要使用$or
和$exists
运算符
{
$match: {
$or: [
{
"existeTransformacion": true
},
{
"existeTransformacion": {
$exists: false
}
}
]
}
}
推荐阅读
- mysql - 按年龄查找数据库中每个人的计数
- firebase - Flutter FirebaseDatabase 运行事务崩溃
- java - 可以运行但不能打包maven spring-boot项目
- python - python 2.7真的快死了吗?
- python - 在matlab中作为索引列出
- sip - 如何在 SIP 引用方法中使用来电显示而不是“s”?
- python - 如果这个变量是全局变量,为什么不定义这个变量?
- php - 如何将 HTML 解析为 PHP?
- ruby-on-rails - Ruby on Rails(Mongoid)中 object.models.find() 与 Model.find() 的成本/收益是什么
- node.js - collection.watch() 不是函数