MongoDB Query (Difference between Two Fields as Conditional Query)

  mongodb, question

field

  • name string
  • x int
  • y int

Table record

name x y
jhon 1 2
lily 2 1
gan 3 2

Query result

Query all name for all x > y

db.collections.find({'$where': "this.x > this.y"}, {'name': 1})