skip to Main Content

my website takes a dataset and the dataset contains data about a students or employees..etc, so I want to display the column names to the user.

Is there a way to pull the fields name?

ex:

Name, Age, Country
String, Number, String
Fahad, 13, riyadh
Jane, 27, United Kingdom
Andrew, 29 , United States
Mary, 19 , France

I want to display "Name" "Age" "Country" to the user

I’m using node.js mongoose.

2

Answers


  1. If your documents have similar structure, why not just

    Object.keys(db.your_collection.findOne())
    Login or Signup to reply.
  2. for more accurate u need use the lean() method because it will
    POJO values else it will return mongodb instance keys instead of document keys.

    Object.keys(db.your_collection.findOne().lean())
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search