2

This query returns a result db.graduates.find({student_id: '2010-01016'}).pretty()

and then I built a function

function findStud(name,value){ return db.graduates.find({name:value}); }

on the mongo shell when I run this findStud("student_id","2010-01016") it does not display the results

Community
  • 1
  • 1
user962206
  • 14,657
  • 59
  • 168
  • 267

1 Answers1

2

You need to compose an query object with the key being the value in the nameparameter and value being the value in the parameter value.

function findStud(name,value){
var query = {};
query[name] = value;
return db.graduates.find(query); 
}

By default when you don't do this, name is considered to be a String literal and the query gets executed as db.graduates.find({"name":value}); which searches for a key named name with the specified value, causing the query to fail.

See Also: Mongodb doesn't not update when I use like this

Community
  • 1
  • 1
BatScream
  • 18,650
  • 4
  • 46
  • 64