[ change based on this is now possible in recent versions]
[Updated answer] You can request the following method of returning the class name and student ID only if they are already registered.
db.student.find({}, {_id:0, name:1, students:{$elemMatch:{$eq:ObjectId("51780f796ec4051a536015cf")}}})
and you will return what you expected:
{ "name" : "CS 101", "students" : [ ObjectId("51780f796ec4051a536015cf") ] } { "name" : "Literature" } { "name" : "Physics", "students" : [ ObjectId("51780f796ec4051a536015cf") ] }
[Original answer] It is not possible to do what you want to do at present. This is unfortunate because you could do this if the student were stored in an array as an object. In fact, I'm a little surprised that you only use ObjectId (), as this will always require you to look for students if you want to display a list of students studying at a particular course (first find the Id list, then look at the names in the student collection - two requests instead of one!)
If you stored (as an example) the identifier and name in an array of courses, for example:
{ "_id" : ObjectId("51780fb5c9c41825e3e21fc6"), "name" : "Physics", "students" : [ {id: ObjectId("51780f796ec4051a536015cf"), name: "John"}, {id: ObjectId("51780f796ec4051a536015d0"), name: "Sam"} ] }
Then your request will be simple:
db.course.find( { }, { students : { $elemMatch : { id : ObjectId("51780f796ec4051a536015d0"), name : "Sam" } } } );
If this student was registered only in CS 101, you will return:
{ "name" : "Literature" } { "name" : "Physics" } { "name" : "CS 101", "students" : [ { "id" : ObjectId("51780f796ec4051a536015cf"), "name" : "John" } ] }