Ⅰ mongodb数据库集合导入导出(迁移)
导出:mongoexport -h IP --port PORT -u USERNAME -p PASSWORD -d DATABASE -c COLLECTION --type=json -o /home/area.json
导入:mongoimport -h IP --port PORT -u USERNAME -p PASSWORD -d DATABASE -c COLLECTION --type=json --file area.json
Ⅱ mongomp导出数据时数据库里是否能查看到记录
mongodb数据备份和还原主要分为二种,一种是针对于库的mongomp和mongorestore,一种是针对库中表的mongoexport和mongoimport。
一,mongomp备份数据库
1,常用命令格
?1 mongomp -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径
如果没有用户谁,可以去掉-u和-p。
如果导出本机的数据库,可以去掉-h。
如果是默认端口,可以去掉--port。
如果想导出所有数据库,可以去掉-d。
Ⅲ mongodb用mongoexport命令导出数据为什么一直显示百分之零
一、导出工具mongoexport
Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示:
Shell代码
[root@localhost mongodb]# ./bin/mongoexport --help
Export MongoDB data to CSV, TSV or JSON files.
options:
--help proce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly 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
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f
name,age
--fieldFile arg file with fields names - 1 per line
-q [ --query ] arg query filter, as a JSON string
--csv export to csv instead of json
-o [ --out ] arg output file; if not specified, stdout is used
--jsonArray output to a json array rather than one object per
line
-k [ --slaveOk ] arg (=1) use secondaries for export if available, default
true
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
实例:test库中存在着一个students集合,集合中数据如下:
Js代码
> db.students.find()
{ "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
由上可以看出文档中存在着3个字段:classid、age、name
1.直接导出数据到文件中
Shell代码
[root@localhost mongodb]# ./bin/mongoexport -d test -c students -o students.dat
connected to: 127.0.0.1
exported 9 records
命令执行完后使用ll命令查看,发现目录下生成了一个students.dat的文件
Shell代码
-rw-r--r-- 1 root root 869 Aug 21 00:05 students.dat
查看该文件信息,具体信息如下:
Shell代码
[root@localhost mongodb]# cat students.dat
{ "_id" : { "$oid" : "5031143350f2481577ea81e5" }, "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : { "$oid" : "5031144a50f2481577ea81e6" }, "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : { "$oid" : "5031145a50f2481577ea81e7" }, "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : { "$oid" : "5031146a50f2481577ea81e8" }, "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : { "$oid" : "5031147450f2481577ea81e9" }, "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : { "$oid" : "5031148650f2481577ea81ea" }, "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : { "$oid" : "5031149b50f2481577ea81eb" }, "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : { "$oid" : "503114a750f2481577ea81ec" }, "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : { "$oid" : "503114cd50f2481577ea81ed" }, "classid" : 2, "age" : 24, "name" : "shane" }
参数说明:
-d:指明使用的库,本例中为test
-c:指明要导出的集合,本例中为students
-o:指明要导出的文件名,本例中为students.dat
从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了JSON格式的数据。如果我们需要导出CSV格式的数据,则需要使用--csv参数,具体如下所示:
Shell代码
[root@localhost mongodb]# ./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat
connected to: 127.0.0.1
exported 9 records
[root@localhost mongodb]# 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
[root@localhost mongodb]#
参数说明:
-csv:指明要导出为csv格式
-f:指明需要导出classid、name、age这3列的数据
由上面结果可以看出,mongoexport成功地将数据根据csv格式导出到了students_csv.dat文件中。
二、导入工具mongoimport
Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示:
Shell代码
[root@localhost mongodb]# ./bin/mongoimport --help
options:
--help proce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly 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
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--fieldFile arg file with fields names - 1 per line
--ignoreBlanks if given, empty fields in csv and tsv will be ignored
--type arg type of file to import. default: json (json,csv,tsv)
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--headerline CSV,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
--stopOnError stop importing at first error rather than continuing
--jsonArray load a json array, not one item per line. Currently
limited to 4MB.
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
示例:先删除students中的数据,并验证
Js代码
> db.students.remove()
> db.students.find()
>
然后再导入上面导出的students.dat文件中的内容
Shell代码
[root@localhost mongodb]# ./bin/mongoimport -d test -c students students.dat
connected to: 127.0.0.1
imported 9 objects
[root@localhost mongodb]#
参数说明:
-d:指明数据库名,本例中为test
-c:指明collection名,本例中为students
students.dat:导入的文件名
查询students集合中的数据
Js代码
> db.students.find()
{ "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
>
证明数据导入成功
上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:
先删除数据
Js代码
> db.students.remove()
> db.students.find()
>
再导入之前导出的students_csv.dat文件
Shell代码
[root@localhost mongodb]# ./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat
connected to: 127.0.0.1
imported 10 objects
[root@localhost mongodb]#
参数说明:
-type:指明要导入的文件格式
-headerline:指明第一行是列名,不需要导入
-file:指明要导入的文件
查询students集合,验证导入是否成功:
Js代码
> db.students.find()
{ "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 }
{ "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 }
{ "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 }
{ "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 }
{ "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 }
{ "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 }
>
Ⅳ mongodb数据库怎么导出数据
用mongodb自带工具 mongoexport,在bin目录下输入:mongoexport --help点击回车可以看到Export MongoDB data to CSV, TSV or JSON files.这样的信息,根据相关参数可以导出成csv的文件
Ⅳ idea的mongo插件把线上的数据库怎么导到本地
本机是Windows 7 32位,故下载的是mongodb-win32-i386-2.6.1.zip,后续例程均是基于该版本数据库。
2、配置环境变量
解压mongodb-win32-i386-2.6.1.zip文件到E:\,并重新命名mongodb-win32-i386-2.6.1文件夹为mongodb,目录结构如下图
并在系统设置中配置环境变量path = E:\mongodb\bin;( 便于在命令行中直接使用mogodb命令 )
3、配置MongoDB数据库
创建一个mongo.config配置文件,配置MongoDB数据库的dbpath(数据库存储路径)和logpath(日志文件存储路径);
你也可是使用 --dbpath选项和 --logpath选项来配置路径;
##store data here
dbpath=E:\mongodb\data
##all output go here
logpath=E:\mongodb\log\mongo.log
特别提示:
由于dbpath路径为 E:\mongodb\data ,而这个文件夹默认不存在,故 需手动创建 ,不然在执行MongoDB服务器时会提示找不到该文件夹;同理,也需 手动创建 一个 E:\mongodb\log 文件夹用于保存日志。
4、启动MongoDB服务器
使用 mongod.exe --config E:\mongodb\mongo.config 启动MongoDB服务器。--config 选项表示启动时通过 E:\mongodb\mongo.config配置文件的信息配置服务器。
5、连接MongoDB服务器
使用mongo.exe连接已经启动的MongoDB server。(如果第4步服务器没有启动成功,连接MongoDB服务器时会报第7步异常里的错误!)
启动成功后,进入mongodb shell命令行,在 mongodb shell命令行 中我们可以进行数据库的访问,已经对数据库执行CRUD操作。
6、添加MongoDB到Winodows服务
window服务来启动MongoDB 服务器。其实做到第5步,我们已经完成了对MongoDB数据的安装已经配置,可以直接开始鼓捣MongoDB CRUD命令
了,但是每次都通过第4步进行添加配置并启动难免麻烦,这才有了第6步添加MongoDB到window服务,实现了通过启动服务来启动MongoDB 服务
器。
1)添加MongoDB到Windows Service, 以便于通过window服务配置系统启动时MongoDB服务自动启动。配置成功后,在控制面板的服务中可看到该服务,如下所示:
C:\Users\Administrator> mongod --config D:\mongodb\mongo.config --install
2)启动MongoDB服务
C:\Users\Administrator> net start MongoDB
3)停止MongoDB服务
C:\Users\Administrator> net stop MongoDB
4)从windows服务中移除MongoDB服务
C:\Users\Administrator> mongod --remove
5)通过mongod --help查看更多的配置命令选项。
C:\Users\Administrator> mongod --help
--install install mongodb service
--remove remove mongodb service
7、异常:
warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061
表示:没有启动MondoDB服务器,或启动服务器失败;
Ⅵ robomongo 怎么导出数据
robomongo 导出数据步骤如下:
1、启动Mongodb数据库。在自己电脑的E盘、D盘根目录模拟出两个Mongodb的数据库,分别代表旧库和新库。
Ⅶ mongoDB怎么把数据导出为csv或excel
1、打开命令行,进入我们所安装的MongoDB路径下的bin文件夹
2、我们采用bin文件夹下的mongoexport方法进行导出,
mongoexport -d myDB -c user -f _id,name,password,adress --csv -o ./user.csv
-d 标示 数据库
-c 标示 数据表
-f 需要提取的field用逗号分隔
-o 输出路径
Ⅷ 使用rockmongo怎么导出部分数据
数据导出:先举个例子作为切入口:
需求:
将test数据库下的things集合中的所有文档导出到D:\mongo_data路径下
D:\mongo\bin>mongoexport -d test -c things -o d:\mongo_data\things.txt
cmd控制台返回导出的相关信息,如下所示
connected to: 127.0.0.1
exported 15 records
检验一下:
去D:\mongo_data找一下是否存在things.txt文件
打开D:\mongo_data\things.txt显示如下:
{ "_id" : 3 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55de" }, "x" : 6, "y" : 0 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55df" }, "x" : 6, "y" : 1 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e0" }, "x" : 6, "y" : 2 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e1" }, "x" : 6, "y" : 3 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e2" }, "add" : [ { "age" : 21, "name" : "jimvin" }, { "age" : 22, "name" : "jimvin" }, { "age" : 23, "name" : "jimvin" }, { "age" : 23, "name" : "jimvin" } ], "x" : 6, "y" : 4 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e3" }, "num" : 55, "x" : 6, "y" : 5 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e4" }, "age" : null, "num" : null, "x" : 6, "y" : 6 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e5" }, "add" : [ "jimvin", "abc", "aaa" ], "num" : "abc", "x" : 6, "y" : 7 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e6" }, "age" : [ 7, 9 ], "name" : "jimvin", "num" : 20, "x" : 6, "y" : 8 }
{ "_id" : { "$oid" : "528ec3042e2ec9f2c3cd55e7" }, "age" : [ 7, 8, 9 ], "name" : "tom", "x" : 10, "y" : 9 }
{ "_id" : { "$oid" : "5296e6d68378a9afba69af46" }, "add" : { "age" : 20, "name" : "jimvin" }, "name" : "jim", "num" : 10 }
{ "_id" : { "$oid" : "529eab9552bf5eb74acdb35b" }, "name" : "person1", "addr" : { "city" : "a", "state" : "d" } }
{ "_id" : { "$oid" : "529eaba252bf5eb74acdb35c" }, "name" : "person1", "addr" : { "city" : "b", "state" : "c" } }
{ "_id" : { "$oid" : "529eabc352bf5eb74acdb35d" }, "name" : "person1", "addr" : { "city" : "a", "state" : "e" } }
从例子我们基本都能猜出 mongoexport的命令用法,下面我们再详细分析一下:
* mongoexport --db
简写 mongoexport -d
指定要导出集合所在的数据库
eg:
D:\mongo\bin>mongoexport --db test
或 D:\mongo\bin>mongoexport -d test
或 D:\mongo\bin>mongoexport -db test
注意:
1. 不能单独使用,至少还要指定集合才能导出成功。
否则显示如下错误:
no collection specified!
2. 假如指定的数据库名在MongoDB中是不存在的,也不会报错的。
* mongoexport --collection
简写 mongoexport -c
指定要导出集合的名字
eg:
D:\mongo\bin>mongoexport --collection test
或 D:\mongo\bin>mongoexport -c test
或 D:\mongo\bin>mongoexport -collection test
注意:
这里是可以正常运行起来的,这里MongoDB系统在没指定数据库来源时,默认从test数据 库找相应的集合的,然后把对应的文档输出到cmd控制台上,假如我们指定的集合名在test数据库是不存在的,也不会报错,只是返回“exported 0 records”这样的提示。
* mongoexport --out
简写 mongoexport -o
指定导出数据文件的目录
eg:
D:\mongo\bin>mongoexport --out d:\mongo_data\things.txt
或 D:\mongo\bin>mongoexport -o d:\mongo_data\things.txt
或 D:\mongo\bin>mongoexport -out d:\mongo_data\things.txt
注意:
1. 不能单独使用,至少还要指定集合才能导出成功。
否则显示如下错误:
no collection specified!
2. 文件目录不能写成d:\mongo_data或 d:\mongo_data\或d:\之类的,否则报错如下:
coundn't open [d:\mongo_data]。
一定要带有文件名的(带有后缀的),一般为.txt,.csv,.json,.csv
csv和csv文件:都是常用的数据交互格式,均可以用excell打开。
3. 纯粹D:\mongo\bin>mongoexport --out d:\mongo_data\things.txt这样写,即使我们没指定集合,但系统依然会为我们创建things.txt这个文件的。
在RockMongo 导出test数据库mythings集合会是一个js文件:
如下所示:
/** mythings indexes **/
db.getCollection("mythings").ensureIndex({
"_id": NumberInt(1)
},[
]);
/** mythings indexes **/
db.getCollection("mythings").ensureIndex({
"location": 1,
"name": -1
},[
]);
/** mythings records **/
db.getCollection("mythings").insert({
"_id": ObjectId("529fe7faeef00d1d48b473c0"),
"location": "Guangzhou",
"age": 20,
"name": "j"
});
db.getCollection("mythings").insert({
"_id": ObjectId("529fe808eef00d1d48b473c1"),
"location": "Guangzhou",
"age": 21,
"name": "ji"
});
......
最后,再重申一点,所有导出操作,必须保证在数据库处于正常连接的状态。否则显示:couldn't conncect to [127.0.0.1] couldn't connect to server 127.0.0.1:27017
Ⅸ mongovue是什么 百度百科
MongoVUE是一款针对MongoDB的客户端工具,现在连接数据库也叫数据模式有2种方法,一种是B/S结构的数据库,通过网页就可以访问。另外一种就是基于C/S客户端的连接方式
Ⅹ 如何从MongoDB导入/导出数据
在开始菜单的运行框中输入dtswiz,然后选择源数据源和目标数据源,例如:如果是从SQLServer中导出到Excel中,那么那么需要在其中输入SQL语句或者选择指定数据库中的一个或多个表,然后再指定Excel的路径和文件名,如果是从Excel导入到SQLServer中,就简单一些,选择好具体的Excel文档后,再选择其中的某一个Sheet(工作表),然后再设置SQLServer的指定数据库即可,还可以从SQLServer中的一个数据库导入到SQLServer的另一个数据库中,方法类似