【问题标题】:MongoDB Query if (Field A - Field B) > NMongoDB 查询是否(字段 A - 字段 B)> N
【发布时间】:2020-07-01 00:45:22
【问题描述】:

我已经坚持了几个小时了。我需要编写一个查询来返回 (Field A - Field B) > N

的所有文档
// sample data
{ _id: '...', estimated_hours: 0, actual_hours: 0 },
{ _id: '...', estimated_hours: 10, actual_hours: 9 },
{ _id: '...', estimated_hours: 20, actual_hours: 30 }

从this stack question 借用答案我写了下面的内容,在我看来这应该可行,但是我一直收到与查询不匹配的记录...

## Attempt 1
n = 0
records = API::Record.where('$where': "(this.estimated_hours - this.actual_hours) > #{n}")
## should return the following, but im getting additional records
#=> [{ _id: '...', estimated_hours: 10, actual_hours: 9 }]

我知道我可以使用$project 完成此操作,但是我必须明确告诉$project 我想要返回哪些字段。我需要返回所有字段,我们使用处理分页的第三方库

【问题讨论】:

  • 我觉得不需要$where: ,试试.where("(this.estimated_hours - this.actual_hours) > #{n}")

标签: ruby mongodb mongoid


【解决方案1】:

play

db.collection.find({
  $where: "(this.estimated_hours - this.actual_hours) > 1"
})

Similar example for reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2013-03-15
    • 2013-11-03
    • 2016-03-17
    相关资源
    最近更新 更多