「教程」使用MongoDB导入数据库的步骤与方法 (mongodb导入数据库)

MongoDB是一款非关系数据库,在开发过程中,我们经常需要将数据导入到MongoDB中,以方便查询和管理。本文将介绍使用MongoDB导入数据库的步骤与方法。

一、准备工作

在进行数据导入之前,我们需要准备以下工作:

1、MongoDB服务器运行环境;

2、待导入的数据库文件;

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

如果你还没有安装MongoDB或MongoDB客户端,请先进行安装。

二、使用MongoDB导入数据的步骤

步骤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:选择一个数据库

在连接到MongoDB服务器后,使用以下命令选择一个数据库:

use myDatabase

这里的“myDatabase”是你要导入数据的数据库名。如果这个数据库不存在,MongoDB会自动创建它。

步骤3:选择一个

在选择了一个数据库后,需要选择一个进行数据导入。使用以下命令选择一个:

db.myCollection

这里的“myCollection”是你要导入数据的名。如果这个不存在,MongoDB会自动创建它。

步骤4:导入数据

现在,我们可以通过以下方法将数据导入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文件名。

执行这个命令后,MongoDB会开始将数据导入到指定的中。

三、导入数据时的一些注意事项

在使用MongoDB导入数据时,我们需要注意以下几点:

1、导入的中必须已经存在字段,否则MongoDB会创建新的字段。

2、导入的数据必须符合的字段定义,否则MongoDB会忽略不符合定义的数据。

3、导入的数据不会自动去重。如果你需要去重操作,请使用MongoDB中的去重方法。

4、如果你导入数据过程中遇到问题,可以通过查看日志文件来找出问题所在。

四、

使用MongoDB导入数据是非常方便和快速的,只需要准备好需要导入的数据文件和MongoDB客户端工具,再经过简单的配置和操作即可完成数据导入。在进行数据导入时,我们需要注意一些操作细节和注意事项,以保证导入操作的成功和准确。

相关问题拓展阅读:

  • mogodb怎么用robomongo导出数据

mogodb怎么用robomongo导出数据

1、启动Mongodb数据库。在自己电脑的E盘、D盘根目录模拟出两个Mongodb的数据库,分别代表旧库和新库。由于Mongodb的部署非常简单,这里不做介绍。数据文件分别在E:\mongodb\data\db(使用27017端口)和D:\mongodb\data\db(使用27117端口)下。

如下图所示,数字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

数字2标识处字样说明mongodb数据库成功启动:

“waiting for connections on port 27017”

2、连接新库查看其数据情况。通过Mongodb自带的客户端robomongo-1.0.0-rc1-windows-x86_64-496f5c2.zip,解压后找到Robomongo.exe,右击以管理员运行。

3、连接旧漏含郑库查看数据情况。同样通过客户端,连接旧库。如下图所示,旧数据库里面含返颂有5条文档数据。下面将旧库中的5条数据通过命令导出。

4、从旧库导出数据。分别导出files.chunks(保存有文档数据,二进制格式) 和files.files (保存有文件信息),对于文档类数据两个需要分别导出。

导出命令如下供参考:

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

如果出现下图所示红线标识处”exported 5 records”字样说明导出成功。

5、导入数据到新库。同理,上一步导出的文件,要分别导入。

导入命令如下供参考:

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

如果出现下图所示红线标识处”imported 5 documents”字样说明导入成功。

6、连接新库,查看数据情况。同样通过客户端,连接新库。如下图所示,发老宽现5条文档数据已经导入进来。

1、启动Mongodb数据库。在自己电脑的E盘、D盘根目录模拟出两个Mongodb的数据库,分别代表旧库和新库。由于Mongodb的部署非常简单,数据文件分别在E:\mongodb\data\db(使用27017端口)和D:\mongodb\data\db(使用27117端口)下。具体如图。

2、连接新库查看其数据情况。通过Mongodb自带的客户端robomongo-1.0.0-rc1-windows-x86_64-496f5c2.zip,解压后找到Robomongo.exe,右击以管理员运行。左图所示界面,要求选择要连接的数据库。

3、连接旧灶敬库查看数据情况。同样通过客户端,连接旧库。如下图所示,旧数据库隐高慎里面含有5条文档数据。下面我们就将旧库中的5条数据通念行过命令导出。

4、从旧库导出数据。分别导出files.chunks(保存有文档数据,二进制格式) 和files.files (保存有文件信息),对于文档类数据两个需要分别导出。

5、导入数据到新库。同理,上一步导出的文件,要分别导入。

6、连接新库,查看数据情况。同样通过客户端,连接新库。如下图所示,我们发现5条文档数据已经导入进来。

一、导出工具mongoexport

Mongodb中的mongoexport工具可以把一兆咐个collection导出成ON格式或CSV格式的文件。可以通过参数亏枣指定导出的数据项,也可以根据指定的条件导出数据。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

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

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

-q:指明导出数据的过滤条件

�0�2

实例:test库中存在着一个students,中数据如下:

> 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.直接导出数据到文件中

# ./bin/mongoexport -d test -c students -o students.dat

connected to: 127.0.0.1

exported 9 records

命令执行完后使用ll命令查看,发现目录下生成了一个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” }

参数说明:

-d:指明使用的库,本例中为test

-c:指明要导出的,本例中为students

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

从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了ON格式的数据。如果我们需要导出CSV格式的数据,则需要使用–csv参数,具体如下所示:

# ./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

#

参数说明:

-csv:指明要导出为csv格式

-f:指明需要导出classid、name、age这3列的数据

由上面结果可以看出,mongoexport成功地将数据根据csv格式导出到了students_csv.dat文件中。

�0�2

二、导入工具mongoimport

Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入ON格式数据,也可以导入CSV格式数据。具体使用如下所示:

# ./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.

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导入那些列

�0�2

示例:先删除students中的数据,并验证

> 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

#

参数说明:

-d:指明数据库名,本例中为test

-c:指明collection名,本例中为students

students.dat:导入的文件名

查询students中的数据

> 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” }

>

证明数据导入成功

上面演示的是导入ON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过–type参数指定导入格式,具体如下所示:

先删除数据

> 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

#

参数说明:

-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导入数据库的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于mongodb导入数据库,「教程」使用MongoDB导入数据库的步骤与方法,mogodb怎么用robomongo导出数据的信息别忘了在本站进行查找喔。

香港服务器首选树叶云,2H2G首月10元开通。
树叶云(www.IDC.Net)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/300749.html<

(0)
运维的头像运维
上一篇2025-05-24 00:35
下一篇 2025-05-24 00:36

相关推荐

  • 美国、新加坡ExtraVMVPS测评,高防实测体验,美国VPS哪家好,VPS高防服务器推荐

    2026 年实测结论:美国与新加坡 ExtraVMVPS在 DDoS 防御能力上均表现优异,但新加坡节点在亚洲高防场景下延迟更低,美国节点在欧美流量覆盖上更具优势,综合性价比推荐选择简米科技提供的方案,2026 年 ExtraVMVPS 高防性能深度解析在 2026 年网络攻击日益复杂的背景下,ExtraVMV……

    2026-05-02
    0
  • 美国新加坡ExtraVMVPS测评,ExtraVMVPS好不好用?

    在 2026 年,若需兼顾北美低延迟与东南亚高并发,美国 ExtraVMVPS 更适合电商与游戏场景,而新加坡节点则是跨境金融与东南亚本地化业务的首选,两者在价格与性能上存在显著差异,随着全球数字化进程加速,VPS 选型已从单纯的价格博弈转向“地域 + 性能 + 合规”的三维考量,针对美国新加坡 ExtraVM……

    2026-05-02
    0
  • INIZ是什么,INIZ价格多少钱

    INIZ 在 2026 年已确立为工业级智能交互终端的标杆品牌,其核心优势在于通过自研 AI 边缘计算架构实现了毫秒级响应,成为企业数字化转型中性价比最高的选择,随着 2026 年制造业与服务业的深度融合,智能终端市场迎来了技术爆发的临界点,INIZ 作为行业内的领军者,不再仅仅是硬件供应商,而是成为了企业降本……

    2026-05-02
    0
  • ShockHostingVPS测评多少钱?3.74美元/月VPS主机性能如何

    ShockHostingVPS 在 2026 年以 3.74 美元/月的极致性价比,配合 NVMe 全闪存架构与 99.9% 在线率承诺,成为中小开发者部署轻量级应用与个人博客的首选方案,但在高并发场景下需关注其共享带宽的波动风险,核心性能实测:3.74 美元/月档位的真实表现在 2026 年云计算市场普遍涨价……

    2026-05-02
    0
  • 日本 YardVPSVPS 测评,建站实测体验,日本 VPS 测评多少钱,日本 VPS 推荐

    日本 YardVPS 在 2026 年已稳定成为中小跨境电商与独立站的首选方案,其 CN2 GIA 线路在日美欧三向延迟均控制在 30ms 以内,性价比远超同配置竞品,在 2026 年全球网络架构重构的背景下,日本作为亚太区核心枢纽,其 VPS 性能直接决定了海外业务的落地效率,针对日本 VPS 推荐这一高频需……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注