新聞中心
MongoDB是一款非關(guān)系數(shù)據(jù)庫,在開發(fā)過程中,我們經(jīng)常需要將數(shù)據(jù)導(dǎo)入到MongoDB中,以方便查詢和管理。本文將介紹使用mongodb導(dǎo)入數(shù)據(jù)庫的步驟與方法。

一、準(zhǔn)備工作
在進(jìn)行數(shù)據(jù)導(dǎo)入之前,我們需要準(zhǔn)備以下工作:
1、MongoDB服務(wù)器運(yùn)行環(huán)境;
2、待導(dǎo)入的數(shù)據(jù)庫文件;
3、MongoDB客戶端命令行工具。
如果你還沒有安裝MongoDB或MongoDB客戶端,請(qǐng)先進(jìn)行安裝。
二、使用MongoDB導(dǎo)入數(shù)據(jù)的步驟
步驟1:通過MongoDB客戶端連接到MongoDB服務(wù)器
使用MongoDB客戶端連接到MongoDB服務(wù)器是進(jìn)行任何MongoDB操作的之一步。在命令行中輸入以下命令:
mongo
輸入命令后,如果連接成功,會(huì)看到類似于以下的輸出:
MongoDB shell version: 4.4.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.4.0
步驟2:選擇一個(gè)數(shù)據(jù)庫
在連接到MongoDB服務(wù)器后,使用以下命令選擇一個(gè)數(shù)據(jù)庫:
use myDatabase
這里的“myDatabase”是你要導(dǎo)入數(shù)據(jù)的數(shù)據(jù)庫名。如果這個(gè)數(shù)據(jù)庫不存在,MongoDB會(huì)自動(dòng)創(chuàng)建它。
步驟3:選擇一個(gè)
在選擇了一個(gè)數(shù)據(jù)庫后,需要選擇一個(gè)進(jìn)行數(shù)據(jù)導(dǎo)入。使用以下命令選擇一個(gè):
db.myCollection
這里的“myCollection”是你要導(dǎo)入數(shù)據(jù)的名。如果這個(gè)不存在,MongoDB會(huì)自動(dòng)創(chuàng)建它。
步驟4:導(dǎo)入數(shù)據(jù)
現(xiàn)在,我們可以通過以下方法將數(shù)據(jù)導(dǎo)入MongoDB中:
1、導(dǎo)入json文件:
下面是一個(gè)用于導(dǎo)入json文件的命令示例:
mongoimport –host localhost –port 27017 –db myDatabase –collection myCollection –file /path/to/jsonfile.json
這里的“jsonfile.json”是你要導(dǎo)入的json文件名。
2、導(dǎo)入csv文件:
下面是一個(gè)用于導(dǎo)入csv文件的命令示例:
mongoimport –host localhost –port 27017 –db myDatabase –collection myCollection –type csv –headerline –file /path/to/csvfile.csv
這里的“csvfile.csv”是你要導(dǎo)入的csv文件名。
執(zhí)行這個(gè)命令后,MongoDB會(huì)開始將數(shù)據(jù)導(dǎo)入到指定的中。
三、導(dǎo)入數(shù)據(jù)時(shí)的一些注意事項(xiàng)
在使用MongoDB導(dǎo)入數(shù)據(jù)時(shí),我們需要注意以下幾點(diǎn):
1、導(dǎo)入的中必須已經(jīng)存在字段,否則MongoDB會(huì)創(chuàng)建新的字段。
2、導(dǎo)入的數(shù)據(jù)必須符合的字段定義,否則MongoDB會(huì)忽略不符合定義的數(shù)據(jù)。
3、導(dǎo)入的數(shù)據(jù)不會(huì)自動(dòng)去重。如果你需要去重操作,請(qǐng)使用MongoDB中的去重方法。
4、如果你導(dǎo)入數(shù)據(jù)過程中遇到問題,可以通過查看日志文件來找出問題所在。
四、
使用MongoDB導(dǎo)入數(shù)據(jù)是非常方便和快速的,只需要準(zhǔn)備好需要導(dǎo)入的數(shù)據(jù)文件和MongoDB客戶端工具,再經(jīng)過簡(jiǎn)單的配置和操作即可完成數(shù)據(jù)導(dǎo)入。在進(jìn)行數(shù)據(jù)導(dǎo)入時(shí),我們需要注意一些操作細(xì)節(jié)和注意事項(xiàng),以保證導(dǎo)入操作的成功和準(zhǔn)確。
相關(guān)問題拓展閱讀:
- mogodb怎么用robomongo導(dǎo)出數(shù)據(jù)
mogodb怎么用robomongo導(dǎo)出數(shù)據(jù)
1、啟動(dòng)Mongodb數(shù)據(jù)庫。在自己電腦的E盤、D盤根目錄模擬出兩個(gè)Mongodb的數(shù)據(jù)庫,分別代表舊庫和新庫。由于Mongodb的部署非常簡(jiǎn)單,這里不做介紹。數(shù)據(jù)文件分別在E:\mongodb\data\db(使用27017端口)和D:\mongodb\data\db(使用27117端口)下。
如下圖所示,數(shù)字1標(biāo)識(shí)處為mongodb的啟動(dòng)命令cmd腳本;
C:\Users\fss>e:
E:\>cd mongodb
E:\mongodb>cd bin
E:\mongodb\bin>mongod –dbpath e:\mongodb\data\db –port 27017
數(shù)字2標(biāo)識(shí)處字樣說明mongodb數(shù)據(jù)庫成功啟動(dòng):
“waiting for connections on port 27017”
2、連接新庫查看其數(shù)據(jù)情況。通過Mongodb自帶的客戶端robomongo-1.0.0-rc1-windows-x86_64-496f5c2.zip,解壓后找到Robomongo.exe,右擊以管理員運(yùn)行。
3、連接舊漏含鄭庫查看數(shù)據(jù)情況。同樣通過客戶端,連接舊庫。如下圖所示,舊數(shù)據(jù)庫里面含返頌有5條文檔數(shù)據(jù)。下面將舊庫中的5條數(shù)據(jù)通過命令導(dǎo)出。
4、從舊庫導(dǎo)出數(shù)據(jù)。分別導(dǎo)出files.chunks(保存有文檔數(shù)據(jù),二進(jìn)制格式) 和files.files (保存有文件信息),對(duì)于文檔類數(shù)據(jù)兩個(gè)需要分別導(dǎo)出。
導(dǎo)出命令如下供參考:
mongoexport -d dbfile -c files.chunks -o 27117_5files_chunks.dat /port 27117
mongoexport -d dbfile -c files.files -o 27117_5files.files.dat /port 27117
如果出現(xiàn)下圖所示紅線標(biāo)識(shí)處”exported 5 records”字樣說明導(dǎo)出成功。
5、導(dǎo)入數(shù)據(jù)到新庫。同理,上一步導(dǎo)出的文件,要分別導(dǎo)入。
導(dǎo)入命令如下供參考:
mongoimport /file:d:\mongodb\bin\27117_5files_chunks.dat /portd dbfile -c files.chunks
mongoimport /file:d:\mongodb\bin\27117_5files.files.dat /portd dbfile -c files.files
如果出現(xiàn)下圖所示紅線標(biāo)識(shí)處”imported 5 documents”字樣說明導(dǎo)入成功。
6、連接新庫,查看數(shù)據(jù)情況。同樣通過客戶端,連接新庫。如下圖所示,發(fā)老寬現(xiàn)5條文檔數(shù)據(jù)已經(jīng)導(dǎo)入進(jìn)來。
1、啟動(dòng)Mongodb數(shù)據(jù)庫。在自己電腦的E盤、D盤根目錄模擬出兩個(gè)Mongodb的數(shù)據(jù)庫,分別代表舊庫和新庫。由于Mongodb的部署非常簡(jiǎn)單,數(shù)據(jù)文件分別在E:\mongodb\data\db(使用27017端口)和D:\mongodb\data\db(使用27117端口)下。具體如圖。
2、連接新庫查看其數(shù)據(jù)情況。通過Mongodb自帶的客戶端robomongo-1.0.0-rc1-windows-x86_64-496f5c2.zip,解壓后找到Robomongo.exe,右擊以管理員運(yùn)行。左圖所示界面,要求選擇要連接的數(shù)據(jù)庫。
3、連接舊灶敬庫查看數(shù)據(jù)情況。同樣通過客戶端,連接舊庫。如下圖所示,舊數(shù)據(jù)庫隱高慎里面含有5條文檔數(shù)據(jù)。下面我們就將舊庫中的5條數(shù)據(jù)通念行過命令導(dǎo)出。
4、從舊庫導(dǎo)出數(shù)據(jù)。分別導(dǎo)出files.chunks(保存有文檔數(shù)據(jù),二進(jìn)制格式) 和files.files (保存有文件信息),對(duì)于文檔類數(shù)據(jù)兩個(gè)需要分別導(dǎo)出。
5、導(dǎo)入數(shù)據(jù)到新庫。同理,上一步導(dǎo)出的文件,要分別導(dǎo)入。
6、連接新庫,查看數(shù)據(jù)情況。同樣通過客戶端,連接新庫。如下圖所示,我們發(fā)現(xiàn)5條文檔數(shù)據(jù)已經(jīng)導(dǎo)入進(jìn)來。
一、導(dǎo)出工具mongoexport
Mongodb中的mongoexport工具可以把一兆咐個(gè)collection導(dǎo)出成ON格式或CSV格式的文件。可以通過參數(shù)虧棗指定導(dǎo)出的數(shù)據(jù)項(xiàng),也可以根據(jù)指定的條件導(dǎo)出數(shù)據(jù)。mongoexport具體用法如下所銷猜拆示:
# ./bin/mongoexport –help
Export MongoDB data to CSV, TSV or ON files.
options:
–help produce help message
-v be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
–version print the program’s version and exit
-h argmongo host to connect to ( /s1,s2 for
sets)
–port arg server port. Can also use –host hostname:port
–ipvenable IPv6 support (disabled by default)
-u arg username
-p arg password
–dbpath argdirectly 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
–directoryperdbif dbpath specified, each db is in a separate
directory
–journal enable journaling
-d argdatabase to use
-c arg collection to use (some commands)
-f argcomma separated list of field names e.g. -f
name,age
–fieldFile argfile with fields names – 1 per line
-q argquery filter, as a ON string
–csvexport to csv instead of json
-o argoutput file; if not specified, stdout is used
–jsonArray output to a json array rather than one object per
line
-k arg (=1) use secondaries for export if available, default
true
參數(shù)說明:
-h:指明數(shù)據(jù)庫宿主機(jī)的IP
-u:指明數(shù)據(jù)庫的用戶名
-p:指明數(shù)據(jù)庫的密碼
-d:指明數(shù)據(jù)庫的名字
-c:指明collection的名字
-f:指明要導(dǎo)出那些列
-o:指明到要導(dǎo)出的文件名
-q:指明導(dǎo)出數(shù)據(jù)的過濾條件
?0?2
實(shí)例:test庫中存在著一個(gè)students,中數(shù)據(jù)如下:
> db.students.find()
{ “_id” : ObjectId(“fea81e5”), “classid” : 1, “age” : 20, “name” : “kobe” }
{ “_id” : ObjectId(“a50fea81e6”), “classid” : 1, “age” : 23, “name” : “nash” }
{ “_id” : ObjectId(“a50fea81e7”), “classid” : 2, “age” : 18, “name” : “james” }
{ “_id” : ObjectId(“a50fea81e8”), “classid” : 2, “age” : 19, “name” : “wade” }
{ “_id” : ObjectId(“fea81e9”), “classid” : 2, “age” : 19, “name” : “bosh” }
{ “_id” : ObjectId(“fea81ea”), “classid” : 2, “age” : 25, “name” : “allen” }
{ “_id” : ObjectId(“b50fea81eb”), “classid” : 1, “age” : 19, “name” : “howard” }
{ “_id” : ObjectId(“503114a750fea81ec”), “classid” : 1, “age” : 22, “name” : “paul” }
{ “_id” : ObjectId(“503114cd50fea81ed”), “classid” : 2, “age” : 24, “name” : “shane” }
由上可以看出文檔中存在著3個(gè)字段:classid、age、name
1.直接導(dǎo)出數(shù)據(jù)到文件中
# ./bin/mongoexport -d test -c students -o students.dat
connected to: 127.0.0.1
exported 9 records
命令執(zhí)行完后使用ll命令查看,發(fā)現(xiàn)目錄下生成了一個(gè)students.dat的文件
-rw-r–r– 1 root rootAug 21 00:05 students.dat
查看該文件信息,具體信息如下:
# cat students.dat
{ “_id” : { “$oid” : “fea81e5” }, “classid” : 1, “age” : 20, “name” : “kobe” }
{ “_id” : { “$oid” : “a50fea81e6” }, “classid” : 1, “age” : 23, “name” : “nash” }
{ “_id” : { “$oid” : “a50fea81e7” }, “classid” : 2, “age” : 18, “name” : “james” }
{ “_id” : { “$oid” : “a50fea81e8” }, “classid” : 2, “age” : 19, “name” : “wade” }
{ “_id” : { “$oid” : “fea81e9” }, “classid” : 2, “age” : 19, “name” : “bosh” }
{ “_id” : { “$oid” : “fea81ea” }, “classid” : 2, “age” : 25, “name” : “allen” }
{ “_id” : { “$oid” : “b50fea81eb” }, “classid” : 1, “age” : 19, “name” : “howard” }
{ “_id” : { “$oid” : “503114a750fea81ec” }, “classid” : 1, “age” : 22, “name” : “paul” }
{ “_id” : { “$oid” : “503114cd50fea81ed” }, “classid” : 2, “age” : 24, “name” : “shane” }
參數(shù)說明:
-d:指明使用的庫,本例中為test
-c:指明要導(dǎo)出的,本例中為students
-o:指明要導(dǎo)出的文件名,本例中為students.dat
從上面的結(jié)果可以看出,我們?cè)趯?dǎo)出數(shù)據(jù)時(shí)沒有顯示指定導(dǎo)出樣式 ,默認(rèn)導(dǎo)出了ON格式的數(shù)據(jù)。如果我們需要導(dǎo)出CSV格式的數(shù)據(jù),則需要使用–csv參數(shù),具體如下所示:
# ./bin/mongoexport -d test -c students –csv -f classid,name,age -o students_csv.dat
connected to: 127.0.0.1
exported 9 records
# cat students_csv.dat
classid,name,age
1.0,”kobe”,20.0
1.0,”nash”,23.0
2.0,”james”,18.0
2.0,”wade”,19.0
2.0,”bosh”,19.0
2.0,”allen”,25.0
1.0,”howard”,19.0
1.0,”paul”,22.0
2.0,”shane”,24.0
#
參數(shù)說明:
-csv:指明要導(dǎo)出為csv格式
-f:指明需要導(dǎo)出classid、name、age這3列的數(shù)據(jù)
由上面結(jié)果可以看出,mongoexport成功地將數(shù)據(jù)根據(jù)csv格式導(dǎo)出到了students_csv.dat文件中。
?0?2
二、導(dǎo)入工具mongoimport
Mongodb中的mongoimport工具可以把一個(gè)特定格式文件中的內(nèi)容導(dǎo)入到指定的collection中。該工具可以導(dǎo)入ON格式數(shù)據(jù),也可以導(dǎo)入CSV格式數(shù)據(jù)。具體使用如下所示:
# ./bin/mongoimport –help
options:
–help produce help message
-v be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
–version print the program’s version and exit
-h argmongo host to connect to ( /s1,s2 for sets)
–port argserver port. Can also use –host hostname:port
–ipv enable IPv6 support (disabled by default)
-u arg username
-p arg password
–dbpath argdirectly 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
–directoryperdbif dbpath specified, each db is in a separate
directory
–journal enable journaling
-d argdatabase to use
-c arg collection to use (some commands)
-f arg comma separated list of field names e.g. -f name,age
–fieldFile argfile with fields names – 1 per line
–ignoreBlanksif given, empty fields in csv and tsv will be ignored
–type argtype of file to import. default: json (json,csv,tsv)
–file argfile to import from; if not specified stdin is used
–drop drop collection first
–headerlineCSV,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
–stopOnErrorstop importing at first error rather than continuing
–jsonArrayload a json array, not one item per line. Currently
limited to 4MB.
參數(shù)說明:
-h:指明數(shù)據(jù)庫宿主機(jī)的IP
-u:指明數(shù)據(jù)庫的用戶名
-p:指明數(shù)據(jù)庫的密碼
-d:指明數(shù)據(jù)庫的名字
-c:指明collection的名字
-f:指明要導(dǎo)入那些列
?0?2
示例:先刪除students中的數(shù)據(jù),并驗(yàn)證
> db.students.remove()
> db.students.find()
>
然后再導(dǎo)入上面導(dǎo)出的students.dat文件中的內(nèi)容
# ./bin/mongoimport -d test -c students students.dat
connected to: 127.0.0.1
imported 9 objects
#
參數(shù)說明:
-d:指明數(shù)據(jù)庫名,本例中為test
-c:指明collection名,本例中為students
students.dat:導(dǎo)入的文件名
查詢students中的數(shù)據(jù)
> db.students.find()
{ “_id” : ObjectId(“fea81e5”), “classid” : 1, “age” : 20, “name” : “kobe” }
{ “_id” : ObjectId(“a50fea81e6”), “classid” : 1, “age” : 23, “name” : “nash” }
{ “_id” : ObjectId(“a50fea81e7”), “classid” : 2, “age” : 18, “name” : “james” }
{ “_id” : ObjectId(“a50fea81e8”), “classid” : 2, “age” : 19, “name” : “wade” }
{ “_id” : ObjectId(“fea81e9”), “classid” : 2, “age” : 19, “name” : “bosh” }
{ “_id” : ObjectId(“fea81ea”), “classid” : 2, “age” : 25, “name” : “allen” }
{ “_id” : ObjectId(“b50fea81eb”), “classid” : 1, “age” : 19, “name” : “howard” }
{ “_id” : ObjectId(“503114a750fea81ec”), “classid” : 1, “age” : 22, “name” : “paul” }
{ “_id” : ObjectId(“503114cd50fea81ed”), “classid” : 2, “age” : 24, “name” : “shane” }
>
證明數(shù)據(jù)導(dǎo)入成功
上面演示的是導(dǎo)入ON格式的文件中的內(nèi)容,如果要導(dǎo)入CSV格式文件中的內(nèi)容,則需要通過–type參數(shù)指定導(dǎo)入格式,具體如下所示:
先刪除數(shù)據(jù)
> db.students.remove()
> db.students.find()
>
再導(dǎo)入之前導(dǎo)出的students_csv.dat文件
# ./bin/mongoimport -d test -c students –type csv –headerline –file students_csv.dat
connected to: 127.0.0.1
imported 10 objects
#
參數(shù)說明:
-type:指明要導(dǎo)入的文件格式
-headerline:指明之一行是列名,不需要導(dǎo)入
-file:指明要導(dǎo)入的文件
查詢students,驗(yàn)證導(dǎo)入是否成功:
> db.students.find()
{ “_id” : ObjectId(“c632cd118ad8”), “classid” : 1, “name” : “kobe”, “age” : 20 }
{ “_id” : ObjectId(“c632cd118ad9”), “classid” : 1, “name” : “nash”, “age” : 23 }
{ “_id” : ObjectId(“c632cd118ada”), “classid” : 2, “name” : “james”, “age” : 18 }
{ “_id” : ObjectId(“c632cd118adb”), “classid” : 2, “name” : “wade”, “age” : 19 }
{ “_id” : ObjectId(“c632cd118adc”), “classid” : 2, “name” : “bosh”, “age” : 19 }
{ “_id” : ObjectId(“c632cd118add”), “classid” : 2, “name” : “allen”, “age” : 25 }
{ “_id” : ObjectId(“c632cd118ade”), “classid” : 1, “name” : “howard”, “age” : 19 }
{ “_id” : ObjectId(“c632cd118adf”), “classid” : 1, “name” : “paul”, “age” : 22 }
{ “_id” : ObjectId(“c632cd118ae0”), “classid” : 2, “name” : “shane”, “age” : 24 }
mongodb導(dǎo)入數(shù)據(jù)庫的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于mongodb導(dǎo)入數(shù)據(jù)庫,「教程」使用MongoDB導(dǎo)入數(shù)據(jù)庫的步驟與方法,mogodb怎么用robomongo導(dǎo)出數(shù)據(jù)的信息別忘了在本站進(jìn)行查找喔。
成都服務(wù)器托管選創(chuàng)新互聯(lián),先上架開通再付費(fèi)。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)-網(wǎng)站建設(shè),軟件開發(fā)老牌服務(wù)商!微信小程序開發(fā),APP開發(fā),網(wǎng)站制作,網(wǎng)站營(yíng)銷推廣服務(wù)眾多企業(yè)。電話:028-86922220
當(dāng)前名稱:「教程」使用MongoDB導(dǎo)入數(shù)據(jù)庫的步驟與方法 (mongodb導(dǎo)入數(shù)據(jù)庫)
分享路徑:http://m.fisionsoft.com.cn/article/dhjcssc.html


咨詢
建站咨詢
