日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
「教程」使用MongoDB導入數(shù)據(jù)庫的步驟與方法 (mongodb導入數(shù)據(jù)庫)

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

一、準備工作

在進行數(shù)據(jù)導入之前,我們需要準備以下工作:

1、MongoDB服務器運行環(huán)境;

2、待導入的數(shù)據(jù)庫文件;

3、MongoDB客戶端命令行工具。

如果你還沒有安裝MongoDB或MongoDB客戶端,請先進行安裝。

二、使用MongoDB導入數(shù)據(jù)的步驟

步驟1:通過MongoDB客戶端連接到MongoDB服務器

使用MongoDB客戶端連接到MongoDB服務器是進行任何MongoDB操作的之一步。在命令行中輸入以下命令:

mongo

輸入命令后,如果連接成功,會看到類似于以下的輸出:

MongoDB shell version: 4.4.0

connecting to: mongodb://127.0.0.1:27017

MongoDB server version: 4.4.0

步驟2:選擇一個數(shù)據(jù)庫

在連接到MongoDB服務器后,使用以下命令選擇一個數(shù)據(jù)庫:

use myDatabase

這里的“myDatabase”是你要導入數(shù)據(jù)的數(shù)據(jù)庫名。如果這個數(shù)據(jù)庫不存在,MongoDB會自動創(chuàng)建它。

步驟3:選擇一個

在選擇了一個數(shù)據(jù)庫后,需要選擇一個進行數(shù)據(jù)導入。使用以下命令選擇一個:

db.myCollection

這里的“myCollection”是你要導入數(shù)據(jù)的名。如果這個不存在,MongoDB會自動創(chuàng)建它。

步驟4:導入數(shù)據(jù)

現(xiàn)在,我們可以通過以下方法將數(shù)據(jù)導入MongoDB中:

1、導入json文件:

下面是一個用于導入json文件的命令示例:

mongoimport –host localhost –port 27017 –db myDatabase –collection myCollection –file /path/to/jsonfile.json

這里的“jsonfile.json”是你要導入的json文件名。

2、導入csv文件:

下面是一個用于導入csv文件的命令示例:

mongoimport –host localhost –port 27017 –db myDatabase –collection myCollection –type csv –headerline –file /path/to/csvfile.csv

這里的“csvfile.csv”是你要導入的csv文件名。

執(zhí)行這個命令后,MongoDB會開始將數(shù)據(jù)導入到指定的中。

三、導入數(shù)據(jù)時的一些注意事項

在使用MongoDB導入數(shù)據(jù)時,我們需要注意以下幾點:

1、導入的中必須已經(jīng)存在字段,否則MongoDB會創(chuàng)建新的字段。

2、導入的數(shù)據(jù)必須符合的字段定義,否則MongoDB會忽略不符合定義的數(shù)據(jù)。

3、導入的數(shù)據(jù)不會自動去重。如果你需要去重操作,請使用MongoDB中的去重方法。

4、如果你導入數(shù)據(jù)過程中遇到問題,可以通過查看日志文件來找出問題所在。

四、

使用MongoDB導入數(shù)據(jù)是非常方便和快速的,只需要準備好需要導入的數(shù)據(jù)文件和MongoDB客戶端工具,再經(jīng)過簡單的配置和操作即可完成數(shù)據(jù)導入。在進行數(shù)據(jù)導入時,我們需要注意一些操作細節(jié)和注意事項,以保證導入操作的成功和準確。

相關問題拓展閱讀:

  • mogodb怎么用robomongo導出數(shù)據(jù)

mogodb怎么用robomongo導出數(shù)據(jù)

1、啟動Mongodb數(shù)據(jù)庫。在自己電腦的E盤、D盤根目錄模擬出兩個Mongodb的數(shù)據(jù)庫,分別代表舊庫和新庫。由于Mongodb的部署非常簡單,這里不做介紹。數(shù)據(jù)文件分別在E:\mongodb\data\db(使用27017端口)和D:\mongodb\data\db(使用27117端口)下。

如下圖所示,數(shù)字1標識處為mongodb的啟動命令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標識處字樣說明mongodb數(shù)據(jù)庫成功啟動:

“waiting for connections on port 27017”

2、連接新庫查看其數(shù)據(jù)情況。通過Mongodb自帶的客戶端robomongo-1.0.0-rc1-windows-x86_64-496f5c2.zip,解壓后找到Robomongo.exe,右擊以管理員運行。

3、連接舊漏含鄭庫查看數(shù)據(jù)情況。同樣通過客戶端,連接舊庫。如下圖所示,舊數(shù)據(jù)庫里面含返頌有5條文檔數(shù)據(jù)。下面將舊庫中的5條數(shù)據(jù)通過命令導出。

4、從舊庫導出數(shù)據(jù)。分別導出files.chunks(保存有文檔數(shù)據(jù),二進制格式) 和files.files (保存有文件信息),對于文檔類數(shù)據(jù)兩個需要分別導出。

導出命令如下供參考:

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)下圖所示紅線標識處”exported 5 records”字樣說明導出成功。

5、導入數(shù)據(jù)到新庫。同理,上一步導出的文件,要分別導入。

導入命令如下供參考:

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)下圖所示紅線標識處”imported 5 documents”字樣說明導入成功。

6、連接新庫,查看數(shù)據(jù)情況。同樣通過客戶端,連接新庫。如下圖所示,發(fā)老寬現(xiàn)5條文檔數(shù)據(jù)已經(jīng)導入進來。

1、啟動Mongodb數(shù)據(jù)庫。在自己電腦的E盤、D盤根目錄模擬出兩個Mongodb的數(shù)據(jù)庫,分別代表舊庫和新庫。由于Mongodb的部署非常簡單,數(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,右擊以管理員運行。左圖所示界面,要求選擇要連接的數(shù)據(jù)庫。

3、連接舊灶敬庫查看數(shù)據(jù)情況。同樣通過客戶端,連接舊庫。如下圖所示,舊數(shù)據(jù)庫隱高慎里面含有5條文檔數(shù)據(jù)。下面我們就將舊庫中的5條數(shù)據(jù)通念行過命令導出。

4、從舊庫導出數(shù)據(jù)。分別導出files.chunks(保存有文檔數(shù)據(jù),二進制格式) 和files.files (保存有文件信息),對于文檔類數(shù)據(jù)兩個需要分別導出。

5、導入數(shù)據(jù)到新庫。同理,上一步導出的文件,要分別導入。

6、連接新庫,查看數(shù)據(jù)情況。同樣通過客戶端,連接新庫。如下圖所示,我們發(fā)現(xiàn)5條文檔數(shù)據(jù)已經(jīng)導入進來。

一、導出工具mongoexport

Mongodb中的mongoexport工具可以把一兆咐個collection導出成ON格式或CSV格式的文件。可以通過參數(shù)虧棗指定導出的數(shù)據(jù)項,也可以根據(jù)指定的條件導出數(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ù)庫宿主機的IP

-u:指明數(shù)據(jù)庫的用戶名

-p:指明數(shù)據(jù)庫的密碼

-d:指明數(shù)據(jù)庫的名字

-c:指明collection的名字

-f:指明要導出那些列

-o:指明到要導出的文件名

-q:指明導出數(shù)據(jù)的過濾條件

?0?2

實例:test庫中存在著一個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個字段:classid、age、name

1.直接導出數(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)目錄下生成了一個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:指明要導出的,本例中為students

-o:指明要導出的文件名,本例中為students.dat

從上面的結果可以看出,我們在導出數(shù)據(jù)時沒有顯示指定導出樣式 ,默認導出了ON格式的數(shù)據(jù)。如果我們需要導出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:指明要導出為csv格式

-f:指明需要導出classid、name、age這3列的數(shù)據(jù)

由上面結果可以看出,mongoexport成功地將數(shù)據(jù)根據(jù)csv格式導出到了students_csv.dat文件中。

?0?2

二、導入工具mongoimport

Mongodb中的mongoimport工具可以把一個特定格式文件中的內容導入到指定的collection中。該工具可以導入ON格式數(shù)據(jù),也可以導入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ù)庫宿主機的IP

-u:指明數(shù)據(jù)庫的用戶名

-p:指明數(shù)據(jù)庫的密碼

-d:指明數(shù)據(jù)庫的名字

-c:指明collection的名字

-f:指明要導入那些列

?0?2

示例:先刪除students中的數(shù)據(jù),并驗證

> db.students.remove()

> db.students.find()

>

然后再導入上面導出的students.dat文件中的內容

# ./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:導入的文件名

查詢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ù)導入成功

上面演示的是導入ON格式的文件中的內容,如果要導入CSV格式文件中的內容,則需要通過–type參數(shù)指定導入格式,具體如下所示:

先刪除數(shù)據(jù)

> db.students.remove()

> db.students.find()

>

再導入之前導出的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:指明要導入的文件格式

-headerline:指明之一行是列名,不需要導入

-file:指明要導入的文件

查詢students,驗證導入是否成功:

> 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導入數(shù)據(jù)庫的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于mongodb導入數(shù)據(jù)庫,「教程」使用MongoDB導入數(shù)據(jù)庫的步驟與方法,mogodb怎么用robomongo導出數(shù)據(jù)的信息別忘了在本站進行查找喔。

成都服務器托管選創(chuàng)新互聯(lián),先上架開通再付費。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)-網(wǎng)站建設,軟件開發(fā)老牌服務商!微信小程序開發(fā),APP開發(fā),網(wǎng)站制作,網(wǎng)站營銷推廣服務眾多企業(yè)。電話:028-86922220


分享標題:「教程」使用MongoDB導入數(shù)據(jù)庫的步驟與方法 (mongodb導入數(shù)據(jù)庫)
本文來源:http://www.dlmjj.cn/article/dhjcssc.html