0

I have been stuck on this for several hours now. I need to write a query that returns all documents where (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 }

Borrowing answers from this stack question I wrote the below, In my mind this should have worked, however I am consistently getting records back that do not match the query...

## 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 }]

I know I can likely accomplish this with $project however i have to explicitly tell $project what fields I want returned. I need all the fields to be returned, we use a third party library that handles pagination

alilland
  • 2,039
  • 1
  • 21
  • 42

1 Answers1

1

play

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

Similar example for reference

Gibbs
  • 21,904
  • 13
  • 74
  • 138