【问题标题】:Mongo DB, how to do restriction on a calculated field?Mongodb,如何对计算字段进行限制?
【发布时间】:2018-07-18 13:56:56
【问题描述】:

刚从 mongo 开始,我正在尝试做一些看起来很简单的事情。

db.getCollection('myCollection').find({ 
    "myProperty" : {
        $gt: 10 - anotherPropertyOfMyObject
    }
})

尝试了 $elemMatch 和投影,但没有骰子。

【问题讨论】:

    标签: mongodb projection


    【解决方案1】:

    您可以分别使用$expr、$gt和$subtract来匹配、查找更大的then和相同文档字段的差异

    db.getCollection('myCollection').find({
      "$expr": { "$gt": [{ "$subtract": [ 10, "$anotherPropertyOfMyObject" ] }, "$myProperty" ] }
    })
    

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 2018-04-20
      • 2016-11-19
      • 1970-01-01
      相关资源
      最近更新 更多