当前位置:网站首页>Mangodb简单使用
Mangodb简单使用
2022-07-27 08:58:00 【MG-net】
一、简介
1、数据模型:


同时可以使用一些脚本进行逻辑的处理
2、特点
- 高性能
- 高可用
- 高扩展
- 丰富的查询方式
- 无模式,数据结构比较松散(BSON存储)
3、配置文件相关内容
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data. 日志存储
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data. 数据存储位置
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs 后台运行
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# timeZoneInfo: /usr/share/zoneinfo
# network interfaces 绑定的IP 和 端口 绑定局域网IP
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
二、基本操作
1、数据库的创建和删除
创建数据库:use dbname,需要注意的是,没有集合的数据库只在内存中存在。
删除数据库:db.dropDataBase()
2、集合的基本操作
在mangobd中,集合相当于关系型数据库的表结构。创建分为隐式、显式创建。
显式创建就是创建没有数据的表,隐式创建就是插入数据的时候直接创建表。
- 单个插入:db.comment.insert({"id":"1","content":"测试"})

- 批量插入:db.comment.insertMany([{"id":"2","content":"测试2","f":"123"},{"id":"3","content":"测试3","y":"123"}]),在插入失败的时候,可以使用try{}catch{}

- 查询所有:db.集合.find()
- 条件查询:db.集合.find({"id":"123"})
- 查询一条:db.集合.findOne(条件)
- 投影查询:db.集合.find({"id":"123"}, {"content":1, "id":0})
- 更新:db.集合.update(query,update,options)
- 覆盖更新:db.comment.update({id:"1"}, {f:NumberInt(2)})
- 局部更新:db.comment.update({id:"2"}, {$set:{f:NumberInt(3)}})
- 批量更新:db.comment.update({id:"2"}, {$set:{f:NumberInt(3)}}, {multi:ture})
- 列值增长:db.comment.update({id:"2"}, {$inc:{f:NumberInt(1)}})
- 删除:db.集合.remove(条件)
- 统计查询:db.集合.count(条件)
- 分页查询:db.集合.find().skip(2).limit(2),第二页每页2行 skip 从0条开始跳多少
- 排序查询:db.集合.find().sort({字段:1}),1升序、-1降序
- 正则表达式查询:db.comment.find({content:/2/})
- 比较查询:db.集合.find({字段:{$gt:700}})
- 包含查询:db.集合.fund({字段:{$in:[v1,v2]}})
- 条件查询:$and、$or,db.comment.find({$and:[{f:{$gt:2}}, {id:{$gt:2}}]})
3、索引-Index
索引就是为了提高效率,没有索引情况下全集合扫描。mongoDB索引使用的时候B-Tree。
索引的类型:
- 单字段索引:在某个字段增加索引

- 复合索引:多个字段的索引

- 其他索引:地理空间索引、文本索引、哈希索引
索引的管理
- 查看:db.集合.getIndexes()
- 创建:db.集合.createIndex(keys, options),keys是增加索引的字段,options如下

- 移除:
- db.集合.dropIndex(索引名称\条件):db.comment.dropIndex({content:-1})
- 删除所有的索引:db.comment.dropIndexs()
执行计划:
用来分析查询语句的执行性能。db.comment.find({f:2}).explain(),使用Compass查看更清楚:

增加索引之后:

涵盖查询:当查询条件和查询的投影仅包含索引字段的时候,直接从索引返回结果,效率很高。

边栏推荐
- 【Flutter -- GetX】准备篇
- What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
- Encountered 7 file(s) that should have been pointers, but weren‘t
- View 的滑动冲突
- Matlab drawing skills and examples: stackedplot
- Deep understanding of Kalman filter (3): multidimensional Kalman filter
- String type and bitmap of redis
- 5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
- [nonebot2] several simple robot modules (Yiyan + rainbow fart + 60s per day)
- 4277. 区块反转
猜你喜欢
![Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1](/img/1c/c1c1b15e502ee901a396840c01e84d.png)
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1

E. Split into two sets

Aruba learning notes 10 security authentication portal authentication (web page configuration)

A survey of robust lidar based 3D object detection methods for autonomous driving paper notes

pollFirst(),pollLast(),peekFirst(),peekLast()

Mmrotate trains its dataset from scratch

NIO this.selector.select()

vscod

General Administration of Customs: the import of such products is suspended

网络IO总结文
随机推荐
3428. 放苹果
2036: [Blue Bridge Cup 2022 preliminary] statistical submatrix (two-dimensional prefix sum, one-dimensional prefix sum)
Hangzhou E-Commerce Research Institute released an explanation of the new term "digital existence"
What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
[nonebot2] several simple robot modules (Yiyan + rainbow fart + 60s per day)
Full Permutation (depth first, permutation tree)
flex布局 (实战小米官网)
Deep understanding of Kalman filter (3): multidimensional Kalman filter
500报错
Flask login implementation
[flutter -- geTx] preparation
3428. Put apples
【微服务~Sentinel】Sentinel之dashboard控制面板
低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择
Linear list (sequential storage, chain storage) (linked list of leading nodes, linked list of non leading nodes)
User management - restrictions
【Flutter -- GetX】准备篇
Matlab 利用M文件产生模糊控制器
Primary function t1744963 character writing
Interface test tool -postman usage details