0

I'm a beginner in both MongoDB and node.js. Actually, I'm trying to get the names of a particular document but I wonder how to do that. Let me elaborate in detail : if "a" is a collection which contains document named "student" and "student" contains fields like

{
    "_id: "student",
    "name": "alex",
    "roll": "3",
    "age":"13"
} 

then how to get the field names in this ["name","roll","age"] format By trying this:

db.collection("user").find({ "_id": "student" }).toArray(function(err, result) {
    console.log(result)
})

is giving all the fields with their values.

Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45

2 Answers2

0

try :

db.collection("user").find({"_id":"student"}, {name:1}).toArray(function(err, result) {console.log(result)}

if you want to exclude fields :

db.collection("user").find({"_id":"student"}, {roll:0, age:0}).toArray(function(err, result) {console.log(result)}

Taki
  • 17,320
  • 4
  • 26
  • 47
0

This will work:

db.collection("user",function(err,user){
   var array = [];
   user.findOne(function(err,doc){
     for (key in doc) array.push(key);
   });
   console.log(array);
});
wrangler
  • 3,454
  • 1
  • 19
  • 28