Home > Back-end >  is there a way to pull only the name of the fields in a document in mongodb using mongoose
is there a way to pull only the name of the fields in a document in mongodb using mongoose

Time:10-18

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.

CodePudding user response:

If your documents have similar structure, why not just

Object.keys(db.your_collection.findOne())

CodePudding user response:

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())
  • Related