summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorDwight <dmerriman@gmail.com>2009-10-12 15:21:41 -0400
committerDwight <dmerriman@gmail.com>2009-10-12 15:21:41 -0400
commit68b99ea200b80a64ec07ccc74900cabe5f640906 (patch)
tree02a470cdc5c8a93d13ea278261edff3ad5bd75d4 /tools
parent81a73d0f63c66a01e2776571ab6d0ebfa302673a (diff)
parentfcd3220d1629d7083645d236ecab143b66901a4c (diff)
downloadmongo-68b99ea200b80a64ec07ccc74900cabe5f640906.tar.gz
Merge branch 'master' of git@github.com:mongodb/mongo
Diffstat (limited to 'tools')
-rw-r--r--tools/import.cpp6
-rw-r--r--tools/tool.cpp63
-rw-r--r--tools/tool.h2
3 files changed, 52 insertions, 19 deletions
diff --git a/tools/import.cpp b/tools/import.cpp
index f88c74323ca..8ba084955b8 100644
--- a/tools/import.cpp
+++ b/tools/import.cpp
@@ -119,9 +119,9 @@ class Import : public Tool {
public:
Import() : Tool( "import" ){
+ addFieldOptions();
add_options()
("type",po::value<string>() , "type of file to import. default: json (json,csv,tsv)")
- ("fields,f" , po::value<string>() , "comma seperated list of field names e.g. -f name,age" )
("file",po::value<string>() , "file to import from; if not specified stdin is used" )
("drop", "drop collection first " )
;
@@ -183,10 +183,6 @@ public:
}
if ( _type == CSV || _type == TSV ){
- if ( ! hasParam( "fields" ) ){
- cerr << "need to speicfy fields for csv and tsv" << endl;
- return -1;
- }
needFields();
}
diff --git a/tools/tool.cpp b/tools/tool.cpp
index 8979c654eaa..e61d6bb70c6 100644
--- a/tools/tool.cpp
+++ b/tools/tool.cpp
@@ -158,22 +158,57 @@ mongo::DBClientBase& mongo::Tool::conn( bool slaveIfPaired ){
return *_conn;
}
+void mongo::Tool::addFieldOptions(){
+ add_options()
+ ("fields,f" , po::value<string>() , "comma seperated list of field names e.g. -f name,age" )
+ ("fieldFile" , po::value<string>() , "file with fields names - 1 per line" )
+ ;
+}
+
void mongo::Tool::needFields(){
- uassert( "you need to specify fields" , hasParam( "fields" ) );
-
- BSONObjBuilder b;
-
- string fields_arg = getParam("fields");
- pcrecpp::StringPiece input(fields_arg);
-
- string f;
- pcrecpp::RE re("([\\w\\.]+),?" );
- while ( re.Consume( &input, &f ) ){
- _fields.push_back( f );
- b.append( f.c_str() , 1 );
+
+ if ( hasParam( "fields" ) ){
+ BSONObjBuilder b;
+
+ string fields_arg = getParam("fields");
+ pcrecpp::StringPiece input(fields_arg);
+
+ string f;
+ pcrecpp::RE re("([\\w\\.]+),?" );
+ while ( re.Consume( &input, &f ) ){
+ _fields.push_back( f );
+ b.append( f.c_str() , 1 );
+ }
+
+ _fieldsObj = b.obj();
+ return;
}
-
- _fieldsObj = b.obj();
+
+ if ( hasParam( "fieldFile" ) ){
+ string fn = getParam( "fieldFile" );
+ if ( ! exists( fn ) )
+ throw UserException( ((string)"file: " + fn ) + " doesn't exist" );
+
+ const int BUF_SIZE = 1024;
+ char line[ 1024 + 128];
+ ifstream file( fn.c_str() );
+
+ BSONObjBuilder b;
+ while ( file.rdstate() == ios_base::goodbit ){
+ file.getline( line , BUF_SIZE );
+ const char * cur = line;
+ while ( isspace( cur[0] ) ) cur++;
+ if ( strlen( cur ) == 0 )
+ continue;
+
+ _fields.push_back( cur );
+ b.append( cur , 1 );
+ }
+ _fieldsObj = b.obj();
+ return;
+ }
+
+ throw UserException( "you need to specify fields" );
}
void mongo::Tool::auth( string dbname ){
diff --git a/tools/tool.h b/tools/tool.h
index c6d2db8780a..18996ec9067 100644
--- a/tools/tool.h
+++ b/tools/tool.h
@@ -70,7 +70,9 @@ namespace mongo {
string _username;
string _password;
+ void addFieldOptions();
void needFields();
+
vector<string> _fields;
BSONObj _fieldsObj;