采用官方工具导出mongo数据为json格式
文档:https://docs.mongodb.com/manual/reference/program/mongoexport/
可以远程导出,只要有host:port即可。
首先需要安装tools:
在redhat中:
文档:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
1、编写repo文件
Create a /etc/yum.repos.d/mongodb-org-3.2.repo file so that you can install MongoDB directly, using yum.
1
2
3
4
5
6
|
[mongodb-org-
3.2
]
name=MongoDB Repository
baseurl=https:
//repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=
1
enabled=
1
gpgkey=https:
//www.mongodb.org/static/pgp/server-3.2.asc
|
2.选择安装功能
一共有以下几种选择
mongodb-org | A metapackage that will automatically install the four component packages listed below. |
mongodb-org-server | Contains the mongod daemon and associated configuration and init scripts. |
mongodb-org-mongos | Contains the mongos daemon. |
mongodb-org-shell | Contains the mongo shell. |
mongodb-org-tools | Contains the following MongoDB tools: mongoimport bsondump, mongodump,mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop. |
如果全部安装:
sudo yum install -y mongodb-org
如果只安装tools
sudo yum install -y mongodb-org-tools
3.导出json
1
2
3
4
5
6
7
8
9
10
11
12
13
|
sudo
mongoexport -h $mongoDBUri --db $db_name -c $collection_name -q '{
$or:[
{
"alerts.FamilyFriendly.completed"
:
false
},
{
"alerts.GasAndSafety.completed"
:
false
},
{
"alerts.Breakfast.completed"
:
false
},
{
"alerts.SpokenLanguage.completed"
:
false
},
{
"alerts.LocationHighlights.completed"
:
false
},
{
"alerts.AccessibilityEnsure.completed"
:
false
},
{
"alerts.AirportShuttle.completed"
:
false
},
{
"alerts.RoomSize.completed"
:
false
},
{
"alerts.Renovations.completed"
:
false
}
]
}' -f hotelId,alerts -o $OUTPUTDIR
/contentScore-
$(
date
+%F)-e.json --jsonArray
|
- -h host:port
- --db db
- -c collections
- -q query string,查询语法
- -f 选择导出字段
- -o 导出文件
- --jsonArray 导出为数组,默认为{}{}{}...
本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/5503228.html,如需转载请自行联系原作者