Using $ where to compare with a nested field

I have a mongodb collection with documents of the following structure (simplifying it, the numbers are just):

{'a': 1, 'b': {'c': 2}}

I want to run the following mongodb request:

{'$ where': 'this.a <this.bc'}

The above does not work. What is the correct syntax for such a query?

+4
source share
1 answer

A problem was detected: not all of my collection documents contained a value of "b", and therefore I received an error: db.alerts.find({$where:"this.a < this.b.c"}) error: { "$err" : "TypeError: Cannot read property 'c' of undefined", "code" : 16722 }

Fixed by changing my request to: {"b.c":{$exists : true}, $where : "this.c < this.b.c"}

+3
source

All Articles