文章目录
MongoDB
一、概述
NoSQL| RDBMS
互联网:高并发、高负载、高容错、快速响应
RDBMS(关系型数据库-传统型):
优点:支持复杂查询,擅长维持关系、支持事务、支持统计分析、磁盘存储
缺点:无法实现快速读写、死板不利于灵活开发、连接数、存储都受限于单机
RDBMS 是一类数据库的总称(Mysql、Oracle、SqlServer、DB2、SQLLite、Access…)
NO SQL:是专门存储非结构化|半结构数据库的总称。
分类:
1)键值对-Redis/SSDB
2)文档类型-MongoDB、CouchDB
3)基于列存储-HBase、BigTable
4)图形化关系存储-Neo4j
NoSQL数据库类型差异化非常大、不同类型数据间一般无法实现互相替换。
NoSQL学习:
1)更多关注每一款NoSQL使用场景(数据结构、指令、架构)
2)轻指令-(查能力) 、重架构-(推荐使用场景)
二、文档类型数据库-MongoDB
(1) MongoDB 基于文档存储NoSQL {id:1,name:"zs"}
BSON有三个特点:轻量性、可遍历性、高效性。
https://blog.csdn.net/m0_38110132/article/details/77716792
(2)MongoDB没有表结构约束-schemaless–集合[文档1,文档2]
(3)支持稀疏存储-记录与记录之间差异性很大 |提升磁盘利用率|灵活
(4)单个文档|记录最大支持16MB数据大小
RBMS:T_USER
id | name | password | sex | info | … |
---|---|---|---|---|---|
1 | zs | 123 | - | - | |
2 | ls | 123 | true | - | |
3 | ww | 123 | - | - |
缺点:表结构过于僵化、空值也会占用存储空间、磁盘利用率低。
NoSQL :t_user
[
{
id:1,name:"zs",password:"123"},
{
id:2,name:"ls",password:"123",sex:true},
{
id:3,name:"ww",password:"123"},
]
优点:表结构比较灵活,支持稀疏存储,磁盘利用率较高
三、基本安装
3.1安装
1)yum安装(rpm)
参考:https://docs.mongodb.com/v3.4/tutorial/install-mongodb-on-red-hat/
创建yum仓库文件
vi /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever(这里填写系统版本)/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
下载安装
yum install -y mongodb-org
2)解压安装(推荐)
MongoDB 归档:https://www.mongodb.org/dl/linux/x86_64
http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.20.tgz
解压至任意目录即可
3.2 Shell启动
-
如果使用yum安装,一般在/usr/local/bin目下,会有mongo相关的命令
-
如果使用下载解压安装在安装解压目录下的bin目录下即可看到相关命令
1)启动指令
./mongod --port 27017 --dbpath=/home/mongodb/data
./mongod -h 打印出帮助命令
Options:
General options:
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying
additional options
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--bind_ip arg comma separated list of ip addresses to
listen on - all local ips by default
--ipv6 enable IPv6 support (disabled by
default)
--maxConns arg max number of simultaneous connections
- 1000000 by default
--logpath arg log file to send write to instead of
stdout - has to be a file, not
directory
--syslog log to system's syslog facility instead
of file or stdout
--syslogFacility arg syslog facility used for mongodb syslog
message
--logappend append to logpath instead of
over-writing
--logRotate arg set the log rotation behavior
(rename|reopen)
--timeStampFormat arg Desired format for timestamps in log
messages. One of ctime, iso8601-utc or
iso8601-local
--pidfilepath arg full path to pidfile (if not set, no
pidfile is created)
--keyFile arg private key for cluster authentication
--noauth run without security
--setParameter arg Set a configurable parameter
--httpinterface enable http interface
--transitionToAuth For rolling access control upgrade.
Attempt to authenticate over outgoing
connections and proceed regardless of
success. Accept incoming connections
with or without authentication.
--clusterAuthMode arg Authentication mode used for cluster
authentication. Alternatives are
(keyFile|sendKeyFile|sendX509|x509)
--nounixsocket disable listening on unix sockets
--unixSocketPrefix arg alternative directory for UNIX domain
sockets (defaults to /tmp)
--filePermissions arg permissions to set on UNIX domain
socket file - 0700 by default
--fork fork server process
--networkMessageCompressors arg Comma-separated list of compressors to
use for network messages
--auth run with security
--jsonp allow JSONP access via http (has
security implications)
--rest turn on simple rest api
--slowms arg (=100) value of slow for profile and console
log
--profile arg 0=off 1=slow, 2=all
--cpu periodically show cpu and iowait
utilization
--sysinfo print some diagnostic system
information
--noIndexBuildRetry don't retry any index builds that were
interrupted by shutdown
--noscripting disable scripting engine
--notablescan do not allow table scans
--shutdown kill a running server (for init
scripts)
Replication options:
--oplogSize arg size to use (in MB) for replication op
log. default is 5% of disk space (i.e.
large is good)
Master/slave options (old; use replica sets instead):
--master master mode
--slave slave mode
--source arg when slave: specify master as
<server:port>
--only arg when slave: specify a single database
to replicate
--slavedelay arg specify delay (in seconds) to be used
when applying master ops to slave
--autoresync automatically resync if slave data is
stale
Replica set options:
--replSet arg arg is <setname>[/<optionalseedhostlist
>]
--replIndexPrefetch arg specify index prefetching behavior (if
secondary) [none|_id_only|all]
--enableMajorityReadConcern enables majority readConcern
Sharding options:
--configsvr declare this is a config db of a
cluster; default port 27019; default
dir /data/configdb
--shardsvr declare this is a shard db of a
cluster; default port 27018
SSL options:
--sslOnNormalPorts use ssl on configured ports
--sslMode arg set the SSL operation mode
(disabled|allowSSL|preferSSL|requireSSL
)
--sslPEMKeyFile arg PEM file for ssl
--sslPEMKeyPassword arg PEM file password
--sslClusterFile arg Key file for internal SSL
authentication
--sslClusterPassword arg Internal authentication key file
password
--sslCAFile arg Certificate Authority file for SSL
--sslClusterCAFile arg CA used for verifying remotes during
outbound connections
--sslCRLFile arg Certificate Revocation List file for
SSL
--sslDisabledProtocols arg Comma separated list of TLS protocols
to disable [TLS1_0,TLS1_1,TLS1_2]
--sslWeakCertificateValidation allow client to connect without
presenting a certificate
--sslAllowConnectionsWithoutCertificates
allow client to connect without
presenting a certificate
--sslAllowInvalidHostnames Allow server certificates to provide
non-matching hostnames
--sslAllowInvalidCertificates allow connections to servers with
invalid certificates
--sslFIPSMode activate FIPS 140-2 mode at startup
Storage options:
--storageEngine arg what storage engine to use - defaults
to wiredTiger if no data files present
--dbpath arg directory for datafiles - defaults to
/data/db
--directoryperdb each database will be stored in a
separate directory
--noprealloc disable data file preallocation - will
often hurt performance
--nssize arg (=16) .ns file size (in MB) for new databases
--quota limits each database to a certain
number of files (8 default)
--quotaFiles arg number of files allowed per db, implies
--quota
--smallfiles use a smaller default file size
--syncdelay arg (=60) seconds between disk syncs (0=never,
but not recommended)
--upgrade upgrade db if needed
--repair run repair on all dbs
--repairpath arg root directory for repair files -
defaults to dbpath
--journal enable journaling
--nojournal disable journaling (journaling is on by
default for 64 bit)
--journalOptions arg journal diagnostic options
--journalCommitInterval arg how often to group/batch commit (ms)
WiredTiger options:
--wiredTigerCacheSizeGB arg maximum amount of memory to allocate
for cache; defaults to 1/2 of physical
RAM
--wiredTigerStatisticsLogDelaySecs arg (=0)
seconds to wait between each write to a
statistics file in the dbpath; 0 means
do not log statistics
--wiredTigerJournalCompressor arg (=snappy)
use a compressor for log records
[none|snappy|zlib]
--wiredTigerDirectoryForIndexes Put indexes and data in different
directories
--wiredTigerCollectionBlockCompressor arg (=snappy)
block compression algorithm for
collection data [none|snappy|zlib]
--wiredTigerIndexPrefixCompression arg (=1)
use prefix compression on row-store
leaf pages
2)shell链接
./mongo # 链接默认端口和本地
-------------------------------------
./mongo --shell 192.168.134.99:27017/baizhi # 连接shell到192.168.134.99 27017数据库下baizhi
-------------------------------------
./mongo --shell --port 27017 --host 192.168.134.99
[root@TestVM bin]# ./mongo --help
MongoDB shell version v3.6.2
usage: ./mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no
'db address' arg expected
--norc will not run the ".mongorc.js" file on
start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)
--disableJavaScriptJIT disable the Javascript Just In Time
compiler
--disableJavaScriptProtection allow automatic JavaScript function
marshalling
--retryWrites automatically retry write operations upon
transient network errors
--jsHeapLimitMB arg set the js scope's heap size limit
Authentication Options:
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
四、基本操作
4.1 基础指令
1)展示所有数据库
show dbs
展示所有的数据库
show databases; 展示所有的数据库
show tables;
> show dbs
admin 0.000GB
local 0.000GB
2)展示当前使用的数据库
db
展示当前使用的数据库
> db
gjf
3)切换数据库
user datbasename
切换到指定的数据库(如果没有则创建,没有数据则无效)
> use gjf
switched to db gjf
4)展示当前库下所有的集合
> show collections
system.version
5)帮助命令
db.help();
帮助命令
> db.help();
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, {
size : ..., capped : ..., max : ... } )
db.createView(name, viewOn, [ {
$operator: {
...}}, ... ], {
viewOptions } )
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {
cmdObj : 1 }
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
4.2 插入操作
https://docs.mongodb.com/v3.4/tutorial/insert-documents/
Mongo官方提示:
CREATING A COLLECTION
If the collection does not currently exist, insert operations will create the collection.
如果集合当前不存在,则插入操作将创建集合。
1)插入单个文档
下面的命令是插入单个文档数据,如果文档未指定_id字段,MongoDB会将带有ObjectId值的_id字段添加到新文档中。详情请查看https://docs.mongodb.com/v3.4/tutorial/insert-documents/#write-op-insert-behavior
> use gjf
switched to db gjf
> db.user.insertOne({
id:1,name:"gjf",age:18})
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8f21d96d7739f7bddc7d86")
}
简单查询
db.user.find()
2)插入多个文档
> db.user.insertMany([{
id:2,name:"rxx",age:18},{
id:2,name:"jzz",age:20},{
id:4,name:"gzy",age:19}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5c8f23a16d7739f7bddc7d87"),
ObjectId("5c8f23a16d7739f7bddc7d88"),
ObjectId("5c8f23a16d7739f7bddc7d89")
]
}
> db.user.find()
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d87"), "id" : 2, "name" : "rxx", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d88"), "id" : 2, "name" : "jzz", "age" : 20 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d89"), "id" : 4, "name" : "gzy", "age" : 19 }
3)通过JS进行插入
for(var i = 1;i<10;i++){
db.user.insertOne({
name:"name"+i,id:i,age:18+i})}
4.3 查询
https://docs.mongodb.com/manual/reference/operator/query/
1)查询所有
> db.user.find()
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d87"), "id" : 2, "name" : "rxx", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d88"), "id" : 2, "name" : "jzz", "age" : 20 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d89"), "id" : 4, "name" : "gzy", "age" : 19 }
2)简单条件查询
指定条件查询,使用key:value形式的条件数据
select * from user where name = ‘gjf’;
> db.user.find({
name:"gjf"})
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
3)使用运算符进行条件查询
(1)in
检索文档下所有年龄等于18或者20 的人
> db.user.find({
age:{
$in:[18,20]}})
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d87"), "id" : 2, "name" : "rxx", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d88"), "id" : 2, "name" : "jzz", "age" : 20 }
此外需要注意的是在对同一字段进行相等性检查时,请使用$in
(尽管使用or也可以)
(2)and
> db.user.find({
id:1,name:"gjf"})
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
基本和查询所有保持一致,and没有特殊表达式。
(3)or
> db.user.find({
$or:[{
id:2},{
name:"gjf"}]})
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d87"), "id" : 2, "name" : "rxx", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d88"), "id" : 2, "name" : "jzz", "age" : 20 }
(4)既有and 又有or
> db.user.find({
age:18,$or:[{
id:2},{
name:"gjf"}]})
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }
{
"_id" : ObjectId("5c8f23a16d7739f7bddc7d87"), "id" : 2, "name" : "rxx", "age" : 18 }
另外:MongoDB支持正则表达式$ regex查询来执行字符串模式匹配。
4)正则匹配查询
(1)$regex
{
< field >: {
$ regex : / pattern / , $ options : '<options>' } }
{
< field >: {
$ regex : 'pattern' , $ options : '<options>' } }
{
< field > : {
$ regex : / pattern / < options > } }
db.user.find({
name:{
$regex:"name"}})
为查询中的模式匹配字符串提供正则表达式功能 。MongoDB使用Perl兼容的正则表达式(即“PCRE”)版本8.41,支持UTF-8。
5)嵌套文档查询
准备数据
db.user.insertMany([
{
id:5,name:"suns",age:18,info:{
h:180,w:130}},
{
id:6,name:"hxz",age:20,info:{
h:185,w:120}},
{
id:7,name:"ergouzi",age:19,info:{
h:190,w:200}}
])
> db.user.find();
{
"_id" : ObjectId("5c8f21d96d7739f7bddc7d86"), "id" : 1, "name" : "gjf", "age" : 18 }