mongodb
Linux 下安装 mongodb
在/etc/yum.repos.d/下创建 mongodb-org-4.2.repo
touch mongodb-org-4.2.repo
vim 写入 下面文件
[ mngodb-org]
name = MongoDB Repository
baseurl = http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.0/x86_64/
gpgcheck = 0
enabled = 1
安装 mongodb
yum install -y mongodb-org
开启 mongodb
systemctl start mongod
设置开机启动 mongodb
systemctl enable mongod
基本常用命令
查看当前数据库
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" } ) ;
投影查询只显示articid
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 ) } } ) ;
包含查询$in
db.collection.find( { "articid" :{ $in :[ 1,3 ] } } ) ;
条件查询$and
db.collection.find( { $and :[
{ "articid" :{ $gt :NumberInt( 1 ) } } ,
{ "articid" :{ $lt :NumberInt( 3 ) } } ,
] } ) ;
条件查询$or
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" )
根据key删除索引
db.collection.dropIndex( { "articid" :1} ) ;
Docker搭建副本集
Docker Compose部署Mongodb副本集群教程
docker pull mongo:4.0.27
编写/home/docker/mongodb/docker-compose.yml配置文件
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
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
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
在路径/home/docker/mongodb启动mongodb 副本集
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( )
master可用性
docker exec -it master mongo
use test
db.test.insert( { userName:"lisi" ,age: 18 } )
secondary可用性
docker exec -it master mongo
use test
rs.secondaryOk( )
db.test.find( )
晓智科技公众号