一、简介
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降序