当前位置:网站首页>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查看更清楚:

增加索引之后:

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

边栏推荐
- 微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
- 4277. Block reversal
- Arm system call exception assembly
- 3311. 最长算术
- The execution sequence of async/await, macro tasks and micro tasks
- Test picture
- NIO this.selector.select()
- 被三星和台积电挤压的Intel终放下身段,为中国芯片定制芯片工艺
- 苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
猜你喜欢

PVT的spatial reduction attention(SRA)

说透缓存一致性与内存屏障

linux安装和远程连接mysql记录

“鼓浪屿元宇宙”,能否成为中国文旅产业的“升级样本”

Network IO summary

Understand various IOU loss functions in target detection

4274. 后缀表达式

4276. Good at C

Intel, squeezed by Samsung and TSMC, finally put down its body to customize chip technology for Chinese chips

A survey of robust lidar based 3D object detection methods for autonomous driving paper notes
随机推荐
Explain cache consistency and memory barrier
TensorFlow损失函数
NiO Summary - read and understand the whole NiO process
How to optimize the deep learning model to improve the reasoning speed
[I2C reading mpu6050 of Renesas ra6m4 development board]
Pyqt5 rapid development and practice 4.1 qmainwindow
Implementation of queue (sequential storage, chain storage)
Query and association of flask to database
CUDA programming-05: flows and events
The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu
Redis network IO
[flutter -- geTx] preparation
693. 行程排序
博客怎么上传动态gif图
Is it safe to buy funds every day? Online and other answers
3428. 放苹果
8 kinds of visual transformer finishing (Part 1)
NiO example
4276. Good at C
Include error in vs Code (new header file)