mongodb中檔案匯入報錯error inserting documents解決方法
技術標籤:mongodbmongodb匯入資料報錯解決linus
相信很多新接觸mongdb的朋友在匯入資料時都或多或少的遇上些許問題,下面就我遇上的問題做個簡單記錄。
安裝完MongoDB會自帶個匯入工具:mongoimport,匯入的指令為
Usage: mongoimport <options> <file> Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin. See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information. general options: --help print usage --version print the tool version and exit verbosity options: -v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N) --quiet hide all log output connection options: -h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets) --port=<port> server port (can also use --host hostname:port) ssl options: --ssl connect to a mongod or mongos that has ssl enabled --sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority --sslPEMKeyFile=<filename> the .pem file containing the certificate and key --sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary --sslCRLFile=<filename> the .pem file containing the certificate revocation list --sslAllowInvalidCertificates bypass the validation for server certificates --sslAllowInvalidHostnames bypass the validation for server name --sslFIPSMode use FIPS mode of the installed openssl library authentication options: -u, --username=<username> username for authentication -p, --password=<password> password for authentication --authenticationDatabase=<database-name> database that holds the user's credentials --authenticationMechanism=<mechanism> authentication mechanism to use namespace options: -d, --db=<database-name> database to use -c, --collection=<collection-name> collection to use input options: -f, --fields=<field>[,<field>]* comma separated list of field names, e.g. -f name,age --fieldFile=<filename> file with field names - 1 per line --file=<filename> file to import from; if not specified, stdin is used --headerline use first line in input source as the field list (CSV and TSV only) --jsonArray treat input source as a JSON array --type=<type> input format to import: json, csv, or tsv (defaults to 'json') ingest options: --drop drop collection before inserting documents --ignoreBlanks ignore fields with empty values in CSV and TSV --maintainInsertionOrder insert documents in the order of their appearance in the input source -j, --numInsertionWorkers=<number> number of insert operations to run concurrently (defaults to 1) --stopOnError stop importing at first insert/upsert error --upsert insert or update objects that already exist --upsertFields=<field>[,<field>]* comma-separated fields for the query part of the upsert --writeConcern=<write-concern-specifier> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority') --bypassDocumentValidation bypass document validation
-h:指明資料庫宿主機的IP
-u:指明資料庫的使用者名稱
-p:指明資料庫的密碼
-d:指明資料庫的名字
-c:指明collection的名字
-f:指明要匯入那些列
例如現在MongoDB中有個庫名為admin,admin資料庫下有個collection名為mongoTest,此時我們要匯入的檔案為:/home/mongo目錄下的mongoData.csv,網上大部分的教程的匯入指令為:
[[email protected] ~]# mongoimport -d admin -c mongoTest /home/mongo/mongoData.csv
直接執行該指令為報如下錯誤:
error inserting documents: not authorized on admin to execute command { insert: "T_ALERT_HOME", documents: 2, writeConcern: { getLastError: 1, w: 1 }, ordered: false }
網上關於這個錯誤的解決方法說得大都不清晰,而且說的基本都是重複的,導致我卡了很多時間,最後才解決。
出現該報錯的原因是由於我們要把資料插入到admin庫,沒有許可權。相信很多朋友都會納悶,在此之前明明已經給admin庫或設定了readWrite許可權甚至是root許可權(一般設定的使用者名稱稱都是admin,
密碼為123456),怎麼可能會沒有許可權執行插入呢?
經過我的測試,原來是在匯入時,MongoDB會對操作的使用者進行檢查,此時我們要把資料匯入到admin庫中,我們需要帶上有許可權的的使用者名稱和密碼(猜測相當於賬號驗證db.auth("admin","123456")),
所以最後我們執行的匯入語句應該如下:
[[email protected] ~]# mongoimport -u admin -p 123456 -d admin -c mongoTest /home/mongo/mongoData.csv
執行成功結果如下:
進入到mongo資料庫admin,檢視mongoTest集合的資料,檢視命令:
db.mongoTest.find().pretty()【加入pretty()是指定用json格式來顯示資料,方便檢視】
檢視總數:db.mongoTest.find().count();
如果出現你匯入的資料,即證明匯入成功
總算解決了,留下該問題的記錄,希望能幫到後面的同學,有問題可以留言,看到會回。
mongo的匯入匯出有個博主寫得比較詳細,附上博文連結: