mongodb
项目 | 地址 |
---|---|
晓智科技 | 晓智科技 |
晓智文档 | 晓智文档 |
源码地址 | 源码地址 |
文档源码 | 文档源码 |
Linux 下安装 mongodb
touch mongodb-org-4.2.repo
[mngodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1
yum install -y mongodb-org
systemctl start mongod
systemctl enable mongod
基本常用命令
- 备注collection:集合名称
db
db.dropDatabase();
db.createCollection("my");
show collections;
db.collection.drop()
增删改查操作
文档的插入
db.collection.inset({ "articid" : "10000", "content" : "天气真好" })
db.collection.insertMany(
[
{"articid":"10001", "content":"天气真好1" },
{"articid":"10002", "content":"天气真好2"}
]);
文档的查询
db.collection.find()
db.collection.findOne({“_id”:"66971f606da08ea6a60473ac"});
db.collection.find({"articid":"10000"},{articid:1})
文档的更新
db.collection.update(query,update,options)
db.collection.update({"articid":"10000"},{"content":"天气好个狗屁"});
db.collection.update({"articid":"10002"},{$set:{"content":"天气好个狗屁"}});
db.collection.update({"articid":"10002"},{$inc:{count:NumberInt(1)}});
文档的删除
db.collection.remove({"articid":"10002"});
db.collection.remove({});
统计与分页
db.collection.count();
db.collection.find().limit(2);
db.collection.find().sort({"articid":-1});
文档的复杂查询
db.collection.find({"content":/小明/});
大于$gt
db.collection.find({"articid":{$gt:NumberInt(1)}});
小于$lt
db.collection.find({"articid":{$lt:NumberInt(2)}});
大于等于$gte
db.collection.find({"articid":{$gte:NumberInt(2)}});
小于等于$lte
db.collection.find({"articid":{$lte:NumberInt(1)}});
不等于$ne
db.collection.find({"articid":{$ne:NumberInt(1)}});
db.collection.find({"articid":{$in:[1,3]}});
db.collection.find({$and:[
{"articid":{$gt:NumberInt(1)}},
{"articid":{$lt:NumberInt(3)}},
]});
db.collection.find({$or:[
{"articid":{$gte:NumberInt(2)}},
{"articid":{$lte:NumberInt(3)}}]
});
索引管理操作
db.collection.getIndexes();
db.collection.createIndex({"articid":1},options);
db.collection.createIndex({"_id":1,"articid":-1},options);
db.collection.dropIndex("_id_1_articid_-1")
db.collection.dropIndex({"articid":1});
Docker搭建副本集
docker pull mongo:4.0.27
version: '3.7'
services:
master:
image: mongo:4.0.27
container_name: master
restart: always
ports:
- 0.0.0.0:27017:27017
volumes:
- /home/data/master:/data/db
command: mongod --dbpath /data/db --replSet appSet --oplogSize 128 # --auth 开启认证
secondary:
image: mongo:4.0.27
container_name: secondary
restart: always
ports:
- 0.0.0.0:27018:27017
volumes:
- /home/data/secondary:/data/db
command: mongod --dbpath /data/db --replSet appSet --oplogSize 128 # --auth 开启认证
arbiter:
image: mongo:4.0.27
container_name: arbiter
restart: always
ports:
- 0.0.0.0:27019:27017
volumes:
- /home/data/arbiter:/data/db
command: mongod --replSet appSet --smallfiles --oplogSize 128
docker-compse up -d
docker exec -it master mongo
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "89888be75898:27017",
"ok" : 1
}
rs.add('47.107.101.79:27018')
rs.add('47.107.101.79:27019',true)
"ok" : 1,
rs.conf()
rs.status()
docker exec -it master mongo
use test
db.test.insert({userName:"lisi",age: 18})
docker exec -it master mongo
use test
# 报错"errmsg" : "not master and slaveOk=false",
rs.secondaryOk()
db.test.find()