Using Variables as fields in a Mongo Query from Node.js06 Aug 2012
And you definitely can, although it isn’t quite as straightforward as putting your variable where the field is supposed to go.
Suppose we have the following code:
This will not accomplish what we want to do. Instead of searching a field ‘name’ for ‘value’, it will search a field ‘searchField’ instead.
What will work though is if we do the following:
And that’s all there is to it. Just create an object, and use the variable name as a key, as shown above.