MongoDB 数据导出
数据导出:先举个例子作为切入口:
需求:
将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
本文来源 我爱IT技术网 http://www.52ij.com/jishu/4748.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
