diff options
author | Aaron <aaron@10gen.com> | 2009-05-20 11:28:55 -0400 |
---|---|---|
committer | Aaron <aaron@10gen.com> | 2009-05-20 11:28:55 -0400 |
commit | 6516e72042cd7e6ff88717b8a9bb5991f223c6c1 (patch) | |
tree | f5ea56d1505afd27cef1bbf491224a98209bc7be /tools | |
parent | 1d091ef8b3cf8063b7e0a111f5ed92eb7536a6de (diff) | |
download | mongo-6516e72042cd7e6ff88717b8a9bb5991f223c6c1.tar.gz |
add option to output to file
Diffstat (limited to 'tools')
-rw-r--r-- | tools/export.cpp | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/tools/export.cpp b/tools/export.cpp index 119f0be6d18..34f8b1da044 100644 --- a/tools/export.cpp +++ b/tools/export.cpp @@ -39,12 +39,18 @@ public: ("query,q" , po::value<string>() , " query filter" ) ("fields,f" , po::value<string>() , " comma seperated list of field names e.g. -f=name,age " ) ("csv","export to csv instead of json") + ("out,o", po::value<string>(), "output file; if not specified, stdout is used") ; } int run(){ const string ns = getNS(); const bool csv = hasParam( "csv" ); + ostream *outPtr = &cout; + string outfile = getParam( "out" ); + if ( hasParam( "out" ) ) + outPtr = new ofstream( outfile.c_str() ); + ostream &out = *outPtr; BSONObj * fieldsToReturn = 0; BSONObj realFieldsToReturn; @@ -79,10 +85,10 @@ public: if ( csv ){ for ( vector<string>::iterator i=fields.begin(); i != fields.end(); i++ ){ if ( i != fields.begin() ) - cout << ","; - cout << *i; + out << ","; + out << *i; } - cout << endl; + out << endl; } while ( cursor->more() ) { @@ -90,15 +96,15 @@ public: if ( csv ){ for ( vector<string>::iterator i=fields.begin(); i != fields.end(); i++ ){ if ( i != fields.begin() ) - cout << ","; + out << ","; const BSONElement & e = obj[i->c_str()]; if ( ! e.eoo() ) - cout << e.jsonString( TenGen , false ); + out << e.jsonString( TenGen , false ); } - cout << endl; + out << endl; } else { - cout << obj.jsonString() << endl; + out << obj.jsonString() << endl; } } |