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

增加索引之后:

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

边栏推荐
- 5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
- What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
- 691. Cube IV
- JS检测客户端软件是否安装
- Pass parameters and returned responses of flask
- General view, DRF view review
- Use of flask
- Storage and computing engine
- 【Flutter -- GetX】准备篇
- Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
猜你喜欢

BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans

PyTorch自定义CUDA算子教程与运行时间分析

杭州电子商务研究院发布“数字化存在”新名词解释

“蔚来杯“2022牛客暑期多校训练营1

async/await的执行顺序以及宏任务和微任务

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

flex布局 (实战小米官网)

Flink1.15 source code reading Flink clients client execution process (reading is boring)

CUDA Programming -03: thread level

Network IO summary
随机推荐
2040: [Blue Bridge Cup 2022 preliminary] bamboo cutting (priority queue)
Activation functions commonly used in deep learning
4277. 区块反转
Redis network IO
CUDA programming-05: flows and events
3311. 最长算术
ctfshow 终极考核
How to study happily on station B?
Horse walking oblique sun (backtracking method)
CUDA programming-01: build CUDA Programming Environment
User management - restrictions
Digital intelligence innovation
2034: [Blue Bridge Cup 2022 preliminary] pruning shrubs
Deep understanding of Kalman filter (2): one dimensional Kalman filter
Tensorflow模型训练和评估的内置方法
The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
Tensorflow package tf.keras module construction and training deep learning model
NiO example
How to optimize the deep learning model to improve the reasoning speed
NIO this.selector.select()