0

I need to show all fields name on the UI from MongoDB collection. I am using NodeJS+Mongoose. Can anyone help me on this?

-Krishna

Krishna Bhatt
  • 251
  • 1
  • 5
  • 18
  • Please edit your question to include what you have implemented and in which part of the implementation you are stuck, otherwise it is too broad. – BatScream Jan 13 '15 at 10:13
  • I have a collection called Contacts which having more than 20 fields which are storing data. I want to show these fields like firstname, lastname etc on the front end. I dnt want to do hard code these fields. Because we change db schema multiple times. if there is any chnage on DB it will reflect automatically. So I need all fields in JSON. WE are using Nodejs and Mongoose. – Krishna Bhatt Jan 13 '15 at 10:36

1 Answers1

1

There is no native function or way to list all fields of a collection because MongoDB is "schemaless". You need to iterate all the documents from the collection and get all the field names. Or simply get the field names from the results of the query you are displaying (like Robomongo).

You can try a MapReduce like the suggest in this post.

I don't recommend to do this very often because is a very expensive operation.

Community
  • 1
  • 1
rsmorros
  • 196
  • 4