mongodb使用总结

mongodb

项目地址
晓智科技晓智科技
晓智文档晓智文档
源码地址源码地址
文档源码文档源码

Linux 下安装 mongodb

  1. 在/etc/yum.repos.d/下创建 mongodb-org-4.2.repo
touch mongodb-org-4.2.repo
  1. 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
  1. 安装 mongodb
yum install -y mongodb-org
  1. 开启 mongodb
systemctl start mongod
  1. 设置开机启动 mongodb
systemctl enable mongod

基本常用命令

  • 备注collection:集合名称
  1. 查看当前数据库
db
  1. 删除当前数据库
db.dropDatabase();
  1. 显示创建集合
db.createCollection("my");
  1. 显示集合
show collections;
  1. 删除集合
db.collection.drop()

增删改查操作

文档的插入

  1. 单条数据插入
db.collection.inset({ "articid" : "10000", "content" : "天气真好" })
  1. 多条数据插入
db.collection.insertMany(
[
    {"articid":"10001", "content":"天气真好1" },
    {"articid":"10002", "content":"天气真好2"}
]);

文档的查询

  1. 查询所有文档的查询
db.collection.find()
  1. 查询单个文档
db.collection.findOne({“_id”:"66971f606da08ea6a60473ac"});
  1. 投影查询只显示articid
db.collection.find({"articid":"10000"},{articid:1})

文档的更新

  1. 修改命令
db.collection.update(query,update,options)
  1. 复盖修改数据
db.collection.update({"articid":"10000"},{"content":"天气好个狗屁"});
  1. 局部修改数据
db.collection.update({"articid":"10002"},{$set:{"content":"天气好个狗屁"}});
  1. 局部自动增长数据
db.collection.update({"articid":"10002"},{$inc:{count:NumberInt(1)}});

文档的删除

  1. 删除单条数据
db.collection.remove({"articid":"10002"})
  1. 批量删除数据
db.collection.remove({});

统计与分页

  1. 文档的统计
db.collection.count();
  1. 分页查询
db.collection.find().limit(2);
  1. 排序查询
db.collection.find().sort({"articid":-1});

文档的复杂查询

  1. 正则查询
db.collection.find({"content":/小明/});
  1. 比较查询
大于$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)}});
  1. 包含查询$in
db.collection.find({"articid":{$in:[1,3]}});
  1. 条件查询$and
db.collection.find({$and:[
    {"articid":{$gt:NumberInt(1)}},
    {"articid":{$lt:NumberInt(3)}},
]});
  1. 条件查询$or
db.collection.find({$or:[
    {"articid":{$gte:NumberInt(2)}},
    {"articid":{$lte:NumberInt(3)}}]
});

索引管理操作

  1. 索引的查看
db.collection.getIndexes();
  1. 创建单值索引
db.collection.createIndex({"articid":1},options);
  1. 创建复合索引
db.collection.createIndex({"_id":1,"articid":-1},options);
  1. 根据名称删除索引
db.collection.dropIndex("_id_1_articid_-1")
  1. 根据key删除索引
db.collection.dropIndex({"articid":1});

Docker搭建副本集

  1. Docker Compose部署Mongodb副本集群教程
docker pull mongo:4.0.27
  1. 编写/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 # --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
  1. 在路径/home/docker/mongodb启动mongodb 副本集
docker-compse up -d
  1. 初始化副本集
docker exec -it master mongo

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "89888be75898:27017",
        "ok" : 1
}
  1. 添加副本集
rs.add('47.107.101.79:27018')
rs.add('47.107.101.79:27019',true) 
"ok" : 1,
  1. 查看配置
rs.conf() 
  1. 查看副本集状态
rs.status()
  1. master可用性
docker exec -it master mongo
use test  
db.test.insert({userName:"lisi",age: 18})
  1. secondary可用性
docker exec -it master mongo
use test
# 报错"errmsg" : "not master and slaveOk=false",
rs.secondaryOk()
db.test.find()
晓智科技公众号
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值