0

I'm wondering if there is a way to specify the field name in a find by passing in a variable. So instead of doing:

db.myCollection.find({fieldName = 123})

doing something like this:

var myVar = "fieldName";
db.myCollection.find({myVar: 123})

I get that this is a weird thing to do, but i'd like to know if this can be done and if so - how?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

1 Answers1

2

Sure - just create an object and pass it in:

var myVar = "fieldName";
var params = {};
params[myVar] = 123;
db.myCollection.find(params)
tymeJV
  • 103,943
  • 14
  • 161
  • 157