MongoDB 使用记录
安装配置
vim /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
yum install -y mongodb-org
service mongod start
# 修改能被外界访问
vim /etc/mongodb.conf
# 启动和开机启动设置
systemctl start mongod
systemctl enable mongod
操作
删除collection
mongo
>use test
switched to db test
> show collections
mycol
mycollection
newcollection
>db.mycollection.drop()
>db.users.find().count()
>db.col.createIndex({"description":-1})
true
排序
索引
# 语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可。
db.collection.createIndex(keys, options)
# 查看集合索引
db.col.getIndexes()
# 查看集合索引大小
db.col.totalIndexSize()
# 删除集合所有索引
db.col.dropIndexes()
# 删除集合指定索引
db.col.dropIndex("索引名称")
去重统计
db.collection_name.distinct(field,query,options)
db.collection_name.distinct(field,query,options).length
field -----指定要返回的字段(string)
query-----条件查询(document)
不同数据库之间表复制
db.threat.find({'unit_id': '90000001'}).forEach(function(d){ db.getSiblingDB('test_center')['threat'].insert(d); });
db.comprehensiveIndexTest.find().forEach(function(d){ db.getSiblingDB('my_visual')['comprehensiveIndex'].insert(d); });
db.basedIndexTest.find().forEach(function(d){ db.getSiblingDB('my_visual')['basedIndex'].insert(d); });
db.fragileIndexTest.find().forEach(function(d){ db.getSiblingDB('my_visual')['fragileIndex'].insert(d); });
db.threadIndexTest.find().forEach(function(d){ db.getSiblingDB('my_visual')['threadIndex'].insert(d); });
数据库复制
db.copyDatabase("phone_record","self_growth","10.9.72.82:27017");
sort()方法的size限制
db.adminCommand({setParameter:1, internalQueryExecMaxBlockingSortBytes:335544320})
模糊查询
db.getCollection('unit').find({"target":/192.168.169.0.24.*/})
导入导出
mongoimport -h 我的IP --port 端口 -d 数据库名 -c 表名 -u 帐号 -p 密码 --upsert /usr/share/nginx/html/xxx.json --authenticationDatabase admin
mongorestore -d db_name 文件夹目录
# 导入json
mongoimport -h 172.19.104.18 --port 20017 -d testdb -c testcol -u mytest -p 12345 --upsert /tmp/test.json --authenticationDatabase testdb
# 导出数据库
mongodump -h 172.19.104.18 --port 20017 -u mytest -p 12345 -d testdb -o /tmp --authenticationDatabase testdb
docker exec -ti mongo mongodump -h 127.0.0.1 --port 27017 -d phone_record -o /home/mongodb/
docker cp mongo:/home/mongodb/ /root/mongo/
docker cp /root/mongo/ mongodb:/home/mongodb/
docker exec -ti mongodb mongorestore -h 127.0.0.1 --port 27017 -u "mongoadmin" -p "secret" --authenticationDatabase admin -d phone_record /home/mongodb/
docker exec -ti mongodb mongorestore --username "mongoadmin" --password "secret" --authenticationDatabase phone_record --db phone_record /home/mongodb/
# 导出表
mongoexport -h 172.19.104.18 --port 20017 -u mytest -p 12345 -d testdb -c testcol -o /tmp/test.json --authenticationDatabase testdb
参考链接