1. 程式人生 > >mongodb 資料匯入匯出若干問題

mongodb 資料匯入匯出若干問題

使用mongoexport.exe匯出資料 , mongoimport.exe 匯出資料

記得命令列不要在末尾加分號 ,也不需要用雙引號將輸出地址包含起來。

如果加了分號,且與分號,則分號也會被當成檔名的一部分。

如果與分號間有空格存在,則會報錯:

ERROR: too many positional options

//匯出 .json檔案

D:\tool\mongo\bin\mongoexport --username user --password 123456 --collection test --db memo --out D:\tool\mongo\backup\outputfile\test.json

匯出json的話,可以不指定-f

預設輸出的資料是一行一條記錄,是JSON物件,各行之間沒有逗號分隔,是以行分隔的。

如果加入--jsonArray 引數,則輸出的是一個包含全部資料的JSON物件

//匯出 .csv檔案

D:\tool\mongo\bin\mongoexport --username user --password 123456 --collection test --db memo -f name,_id,age,addr,di --csv --out D:\tool\mongo\backup\outputfile\test.csv

此格式需要指定-f 即輸出的欄位,以逗號隔開,否則會報如下錯誤:
assertion: 9998 you need to specify fields

如果沒寫-f 雖然也能輸出一個CSV檔案,但是是空檔案。

-f中如果寫了不存在的欄位,該欄位能夠輸出,但為空值

//匯入

D:\tool\mongo\bin\mongoimport.exe -d memo -c jfmall --file D:\tool\mongo\backup\outputfile\test.json -u user -p 123456

如果加入引數-q指定一個查詢條件,需要使用單引號括起來,如下所示:


/mongoexport -d page -c Article -q '{"title": "cc"}' -f _id,title,content,images,url --jsonArray > mafengwoArticle.txt  
否則,就會出現下面的錯誤:


ERROR: too many positional options  

 
mongoexport --help  
options:  
  --help                  produce help message  
  -v [ --verbose ]        be more verbose (include multiple times for more verbosity e.g. -vvvvv)  
  -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
  --port arg              server port. Can also use --host hostname:port  
  --ipv6                  enable IPv6 support (disabled by default)  
  -u [ --username ] arg   username  
  -p [ --password ] arg   password  
  --dbpath arg            directly access mongod database files in the given  
                          path, instead of connecting to a mongod  server -  
                          needs to lock the data directory, so cannot be used  
                          if a mongod is currently accessing the same path  
  --directoryperdb        if dbpath specified, each db is in a separate directory  
  -d [ --db ] arg         database to use  
  -c [ --collection ] arg collection to use (some commands)  
  -f [ --fields ] arg     comma separated list of field names e.g. -f name,age  
  --fieldFile arg         file with fields names - 1 per line  
  -q [ --query ] arg      query filter, as a JSON string  
  --csv                   export to csv instead of json  
  -o [ --out ] arg        output file; if not specified, stdout is used  
  --jsonArray             output to a json array rather than one object per line  
mongoimport --help  
options:  
  --help                  produce help message  
  -v [ --verbose ]        be more verbose (include multiple times for more  
                          verbosity e.g. -vvvvv)  
  -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
  --port arg              server port. Can also use --host hostname:port  
  --ipv6                  enable IPv6 support (disabled by default)  
  -u [ --username ] arg   username  
  -p [ --password ] arg   password  
  --dbpath arg            directly access mongod database files in the given  
                          path, instead of connecting to a mongod  server -  
                          needs to lock the data directory, so cannot be used  
                          if a mongod is currently accessing the same path  
  --directoryperdb        if dbpath specified, each db is in a separate  
                          directory  
  -d [ --db ] arg         database to use  
  -c [ --collection ] arg collection to use (some commands)  
  -f [ --fields ] arg     comma separated list of field names e.g. -f name,age  
  --fieldFile arg         file with fields names - 1 per line  
  --ignoreBlanks          if given, empty fields in csv and tsv will be ignored  
  --type arg              type of file to import.  default: json (json,csv,tsv)  
  --file arg              file to import from; if not specified stdin is used  
  --drop                  drop collection first  
  --headerline            CSV,TSV only - use first line as headers  
  --upsert                insert or update objects that already exist  
  --upsertFields arg      comma-separated fields for the query part of the  
                          upsert. You should make sure this is indexed  
  --stopOnError           stop importing at first error rather than continuing  
  --jsonArray             load a json array, not one item per line. Currently  
                          limited to 4MB.