「教程」使用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

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

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