0

My requirement is to print all the data from a collection to a CSV file, but I don't know the name of all the fields.

When I am using the following format, I get the error

mongoexport -h ServerName -p PortName -c CollectionName -csv "C:\data.csv" -u UserId -p Password

ERROR: too many positional options

To use CSV, one need to specify --fields or --fieldFile...for which I need to know the name of all possible fields in a given collection.

Anybody knows how to get all the fieldnames of a collection ? Appreciate the help. Thanks.

rajibdotnet
  • 1,498
  • 2
  • 17
  • 29
  • possible duplicate of [What does "too many positional options" mean when doing a mongoexport?](http://stackoverflow.com/questions/7521163/what-does-too-many-positional-options-mean-when-doing-a-mongoexport) – WiredPrairie Feb 14 '14 at 20:15
  • It's actually an oddity of the command line tool if you look at the linked duplicate question's answer. – WiredPrairie Feb 14 '14 at 20:15
  • No I don't think its the misbehavior of the tool. Basically it boils down to questions...How to pull all the distinct columns from a collection ? – rajibdotnet Feb 14 '14 at 20:46
  • 1
    You're getting the error likely because you have a space between the `-p` and the `password`. – WiredPrairie Feb 14 '14 at 20:50
  • Doesnot help removing the space. Also I corrected -p for port. mongoexport -h ServerName -port PortName -c CollectionName -csv "C:\data.csv" -u UserId -p Password – rajibdotnet Feb 14 '14 at 21:06
  • I am rephrasing my question below :- How to pull all the distinct columns from a collection ? – rajibdotnet Feb 14 '14 at 21:07
  • 1
    You still show a space in the command line around password. There are already answers to both questions you've got on StackOverflow and other web sites. – WiredPrairie Feb 14 '14 at 21:54
  • First of all...space in Passpord have no effect. I have already verified that. If you do mongoexport -h you can see that too. Second, none of the other posts answer my question "How to pull all the distinct columns from a collection"; especially when my documents have n depths. – rajibdotnet Feb 14 '14 at 22:03
  • Try this one:http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection – heinob Feb 15 '14 at 03:27
  • specify DB name (-db DB_NAME) – Danish Feb 15 '14 at 11:44

1 Answers1

0

You can use a map-reduce or a schema analyzer tool like variety to determine the set of columns in a collection.

A map-reduce giving you a frequency count of each field name in the collection:

db.collection.mapReduce(
    function() { for (var key in this) { emit(key, 1); } },
    function(key, values) { return Array.sum(values); },
    {out: {inline: 1}}
)
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471