2

Is it possible to do a comparison (e.g. greater than, ">") between two properties in a MongoDB document?

For example, if I had a document:

{
   "t" : [100, 200]
}

Is it possible to do something like this to find documents with the first item in "t" to be less than the second item in "t"?

db.item.find( {"t.0": {$lte: "t.1"} } );

I realize I can first query all relevant documents first and then do my filtering after in my app, but wanted to see if there's a native way in MongoDB to do this.

Appreciate your help, Josh

jchan
  • 55
  • 1
  • 5
  • While it isn't an exact duplicate, I think the first part of the accepted answer to https://stackoverflow.com/questions/20165272/mongodb-find-comparing-array-elements may solve your problem. – Wayne Tanner Feb 13 '15 at 18:26

1 Answers1

2

You cant do it in any find operations. but you can use javascript:

db.item.find({ $where : "this.t[0] > this.t[1]" });
Disposer
  • 6,201
  • 4
  • 31
  • 38
  • Perfect. Great reminder that many query solutions in MongoDB can be achieved directly through JS. – jchan Feb 13 '15 at 18:31