summaryrefslogtreecommitdiff
path: root/tools/export.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/export.cpp')
-rw-r--r--tools/export.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/tools/export.cpp b/tools/export.cpp
index aabebf3..5603823 100644
--- a/tools/export.cpp
+++ b/tools/export.cpp
@@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "stdafx.h"
+#include "pch.h"
#include "client/dbclient.h"
#include "db/json.h"
@@ -34,17 +34,20 @@ namespace po = boost::program_options;
class Export : public Tool {
public:
Export() : Tool( "export" ){
+ addFieldOptions();
add_options()
("query,q" , po::value<string>() , "query filter, as a JSON string" )
- ("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")
+ ("jsonArray", "output to a json array rather than one object per line")
;
+ _usesstdout = false;
}
int run(){
string ns;
const bool csv = hasParam( "csv" );
+ const bool jsonArray = hasParam( "jsonArray" );
ostream *outPtr = &cout;
string outfile = getParam( "out" );
auto_ptr<ofstream> fileStream;
@@ -76,7 +79,7 @@ public:
auth();
- if ( hasParam( "fields" ) ){
+ if ( hasParam( "fields" ) || csv ){
needFields();
fieldsToReturn = &_fieldsObj;
}
@@ -99,6 +102,9 @@ public:
out << endl;
}
+ if (jsonArray)
+ out << '[';
+
long long num = 0;
while ( cursor->more() ) {
num++;
@@ -115,10 +121,18 @@ public:
out << endl;
}
else {
- out << obj.jsonString() << endl;
+ if (jsonArray && num != 1)
+ out << ',';
+
+ out << obj.jsonString();
+
+ if (!jsonArray)
+ out << endl;
}
}
+ if (jsonArray)
+ out << ']' << endl;
cerr << "exported " << num << " records" << endl;