目录
需要进入到mongo的bin目录下
mongo导出
mongodump备份
./mongodump -h 127.0.0.1 --port 27017 -u tbook -p tbook -d STC -c tbookKLine_new_bak_20221021 -o /opt/sumscope/mongodb-back/
值 | 解释 |
---|---|
mongodump | 导出命令 |
-h | mongo地址 |
–port | mongo端口 |
-u | 账号 |
-p | 密码 |
-d | 数据库 |
-c | 数据集合 |
-o | 导出地址 |
mongoexport 数据导出
json 导出
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test -o /opt/test.json
csv 导出
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test -o test.csv
mongoexport 数据导出指定字段
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test -f "name,sex" -o test.json
mongoexport 数据导出指定字段并添加查询条件
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test -f "name,sex" -q '{"time":{"$gt":"2022-10-18 17:45:06.460"}}' -o test.json
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test -f "name,sex" -q '{"sex":"男"}' -o test.json
mongoexport 日期区间导出
首先需要创建一个文件,把查询条件放入其中
如:
{dateTime:{'$gt':'2022-10-26 00:00:00.000','$lt':'2022-10-26 23:59:59.999'}}
导出数据:指定查询文件 –queryFile
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c tbookBboMarket --queryFile /opt/sumscope/mongodb/bin/test.text --type=json -o test.json
导出结果:
mongoexport pretty输出展示
全部展示
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test --pretty -o test.json
指定查询条件展示
./mongoexport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test -f "name,sex" -q '{"time":{"$gt":"2022-10-18 17:45:06.460"}}' --pretty -o test.json
mongo导入
mongorestore 数据恢复
./mongorestore -h 127.0.0.1 --port 27017 --authenticationDatabase STC -u tbook -p tbook -d STC -c tbookKLine_new /opt/sumscope/mongodb-back/STC/tbookKLine_new_bak_20221021.bson
值 | 解释 |
---|---|
mongorestore | 导入命令 |
-h | mongo地址 |
–port | mongo端口 |
–authenticationDatabase | 数据库认证 |
-u | 账号 |
-p | 密码 |
-d | 数据库 |
-c | 数据集合 |
-o | 导入文件地址 ,导出后会出现bosn文件,直接进行bons导入 |
mongoimport 数据导入
json
./mongoimport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test --type=json --file /opt/test.json
csv
./mongoimport --host 127.0.0.1:27017 -u tbook -p tbook -d STC -c test --type=json --file /opt/test.csv
mongo集合合并
mongo客户端集合合并
db.tbookKLine_new_bak_20221021.find().forEach(function(item) {
db.tbookKLine_new.insert(item);
})
mongo先删除重复的在添加
db.tbookKLine_new_bak_20221021.find().forEach(function(item) {
#删除已有的数据
db.tbookKLine_new.remove(item);
#添加现有的数据
db.tbookKLine_new.insert(item);
})
值 | 解释 |
---|---|
tbookKLine_new_bak_20221021 | 准备合并的集合 |
tbookKLine_new | 要合并到的集合 |