I have been using MongoDB and the Ruby driver and Mongoid, and lines
db.things.find({j: {$ne: 3}, k: {$gt: 10} });
just seem so weird and hard to remember. Why not use a parser:
db.things.find("j != 3 && k > 10")
which can automatically convert to its desired form? (or whatever form it uses internally).
MongoDB supports JavaScript expressions in
find()statements. Just be aware that:The JavaScript expression string is parsed into actual JavaScript once, which is then evaluated for each document.
However, the JavaScript is not converted to native operators, such as
{ $ne: 3 }. The reason for this is that not all JavaScript can be expressed using native operators.Because it cannot convert the expression into native operators, it (probably) also doesn’t know which indexes to use. As a result, JavaScript expressions can be somewhat slower than native operators.