Node.js 操作Mongodb

全面指南:使用Node.js和Python连接与操作MongoDB 在现代Web开发中,数据库是存储和管理数据的核心组件。MongoDB作为一款流行的NoSQL数据库,以其灵活的数据模型、高性能和易扩展性广受开发者欢迎。无论是使用Node.js还是Python,MongoDB都提供了强大的官方驱动和第三方库,使得数据库操作变得简单高效。 阅读详情
                   Node.js 操作Mongodb
1.简介
官网英文文档  https://docs.mongodb.com/manual/  这里几乎什么都有了
MongoDB is open-source document database that provides high performance , high availability , and automatic scaling.
MongoDB是一个开源的文档数据库,提供高性能、高可用性、自动缩放
2.安装
详细的安装相关信息,比如支持哪些系统,32位和64位有哪些区别,这些请上官网自行查看

Sudo apt-get install –y mongodb-org
安装mongodb-org 会依赖于
mongodb-org-server  包含mongod进程,配置文件还有初始化脚本
mongodb-org-mongos 包含mongos进程
mongodb-org-shell    包含mongo命令
mongodb-org-tools    包含mongoinport ,mongodump,mongorestore,mongofiles等命令
各个命令有什么用,请看下章。

启动mongodb
sudo mongod
启动的时候有一些参数,可以通过mongod –help查看
默认端口为27017,你也可以通过mongod  --port=xxx 来改变端口。
一般常用的是 mongod  -f /etc/mongodb.config 来启动   mongodb.config如何配置请
停止mongodb
官网推荐的是 mongo 进到命令行里
use admin   //转到admin数据库
db.shutdownServer()   //停止mongodb
也可以直接kill mongodb的进程
ps –ef|grep mongodb   找到mongodb的进程pid
Kill -9 pid

3.命令
启动mongodb命令
cd mongodbHome
./bin/mongo   具体参数可以用 ./bin/mongo --help查看 如下
help          显示帮助
show dbs      显示数据库列表
use <db>      转到某个数据库
show collections  显示当前数据库所包含的集合(类似mysql中的表)
show users     显示当前数据库的用户列表
……           …….
……           …….
还有很多功能,这里就不列
4.增删改查
1.新增
db.collection.insert({x:”a”,y:”b”,z:”c”})
意思是向collection集合中插入一条数据,格式是x:”a”,y:”b”,z:”c”  这个格式类似json,可以自定义,collection是集合名称,比如向user集合中插入数据{name=”老王”,info=”老王是一种神秘的生物,他往往就住在你隔壁”}可以这么写
db.user.insert({name:”老王”,info:”老王是一种神秘的生物,他往往就住在你隔壁”})
mongodb3.2版还增加了 db.collection.insertOne()和db.collection.insertMany() 两个方法
db.collection.insertMany([
   {x:”a”,y:”b”,z:”c”},
   {x:”1”,y:”2”,z:”3”},
   {……………………….},
          …
          …
          …
])
这个方法效率很高。使用命令行的时候,可以先设置一个变量A=[{…},{…},{…},{…}]
在使用db.collection.insertMany(A)
2.删除
db.collection.remove({x:”a”})
意思是删除collection集合中x=”a”的所有数据
   3.修改
       db.collection.update({
           {x:”a”},
           {$set{y:”1”}},
           {multi:true}     //true:匹配多行 false:匹配一行 默认为false
})
相当于  update collection set y=”1” where x=”a”
4.查询     mongodb语句        对应sql语句
   db.collection.find()        select * from collection
   db.collection.find({x:”a”})   select * from collection where x=”a”;
   db.collection.find({x:”a”,z:{$lt:”3”}})   select * from collection where x=”a” and z<3
   db.collection.find({x:”a”,$or{z:{$lt:”3”}}})  select * from collection where x=”a” or z<3
   db.collection.find({x:”a”},{x:1,y:0,z:1})  select x,z from collection where x=”a”
 
   游标
   var myCursor=db.collection.find();
   while(myCursor.hasNext()){
      print(myCursor.next())
}
更多高级应用请查阅 db.collection.bulkWrite()
Mongodb命令与sql的对比请移步至 https://docs.mongodb.com/manual/reference/sql-comparison/  查看
5.数据处理
1.Aggregation
 Aggregations operations process data records and return computed results
Aggregation处理数据记录并返回处理结果,类比于关系数据库中的函数,存贮过程之类。限制是返回的数据要<16M   
假设collecion中包含x,y,z
db.collection.aggregate([
{$match:{x:”a”}},
{$group:{_id:$y,total:{$sum:z}}} 
])
说明:$group一定要有_id,这个_id可以是collection里边的某个列名,等价于
select y as _id,sum(z) as total from aggregate where x=”a” group by y
$match,$group,$limit,$sort,等方法请参考https://docs.mongodb.com/manual/meta/aggregation-quick-reference/
Aggregation与sql的对应关系请参考
https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/

2.Map-Reduce
 Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results
大概意思是Map-reduce能对大数据量进行处理,并返回处理的结果
db.collection.mapReduce(
             function(){             
emite(this.x,this.z);        //map
},
             function(key,values){
                return Array.sum(values);   //reduce
},
             {
               query:{y:”b”},             //query
               out:”test”                //output to collection name test
             }
)
6.数据模型
单文档操作是原子性的,支持事务
多文档操作是并发的,不支持事务
设置文档验证模型
db.createCollection(“contacts”,
  {  validator:{[
      {phone:{$type:”string”}},
      {email:{$regex:/@qq.com$/}}
     ]}
})
db.runCommand(
  {
    collMod:”contacts”,
    validator:{[
         {phone:{$type:”string”}},
         {email:{$regex:/@qq.com$/}}
       ]}
      validationLevel:”moderate”
  }
)
You cannot specify a validator for collections in the admin, local, and config databases.
注意:您无法对admin,local,config数据库使用文档验证模型
7.管理
1.多线程
mmapv1 provides collection-level locking  
意思是同一时间对collection里边的document操作只能有唯一一个读或写
wiredTiger supports concurrent access by readers and writers to the documents in a collection. Clients can read documents while write operations are in progress, and multiple threads can modify different documents in a collection at the same time
意思是wiredTiger支持同时对某一文档进行读写,同时对某一集合下的不同文档进行修改,说白了就是wiredTiger是document-level locking 且读写对document可同时存在.
2.数据持久化
MongoDB uses write ahead logging to an on-disk journal. Journaling guarantees that MongoDB can quickly recover write operations that were written to the journal but not written to data files in cases where mongod terminated due to a crash or other serious failure.
Mongodb 使用wal日志(写之前先记录到日志中)来保证快速恢复。
3.硬件要求
wiredTiger越多的cpu核心越好,最少2核心.
Mongodb3.2默认使用60%的内存,且最小1GB,推荐是单机内存10G
启动时可以通过mongod –wiredTigerCacheSizeGB=xxx 来设置使用多少内存
wiredTigerCacheSizeGB: Defines the maximum size of the internal cache that WiredTiger will use for all data.

With the WiredTiger storage engine, use of XFS is strongly recommended to avoid performance issues that may occur when using EXT4 with WiredTiger
使用wiredTiger引擎强烈建议使用xfs文件系统。xfs文件系统比ext4文件系统对mongodb更友好

集群的时候要保证时间同步(NTP)
4.性能优化
For read-heavy applications, increase the size of your replica set and distribute read operations to secondary members.

For write-heavy applications, deploy sharding and add one or more shards to a sharded cluster to distribute load among mongod instances.
针对读性能优化是增加replication(备份)来分担读压力
针对写性能优化是增加sharding(分片)来减轻写压力

//对某个数据库使用慢操作日志
db.setProfilingLevel(0,100),设置这个以后就可以查看相应的慢操作了
0:关闭 默认
1:打开 仅记录慢操作 
2:打开 记录所有操作
慢操作定义为100毫秒
查看慢操作  db.system.profile.find( { millis : { $gt : 100 } } )
查看profile的定义 db.getProfilingStatus()
//对所有数据库使用慢操作日志
mongod --profile 1 --slowms 15
5.配置文件
设置查询时间 db.collection.find().maxTimeMS(30)
设置命令时间 db.runCommand(
                {   distinct:”collection”,
                    key:”a”,
                    maxTimeMS:40
                 }
}
        获取当前操作 db.currentOp()
        停止当前操作 db.killOp(<opid>)
6.备份
最简单的备份是使用MongoDB Cloud Manager 或者 Ops Manager

Back Up with Filesystem Snapshots   文件快照备份
You can create a backup of a MongoDB deployment by making a copy of MongoDB’s underlying data files.   直接复制数据文件
文件快照要求 To get a correct snapshot of a running mongod process, you must have journaling enabled  必须打开journaling
步骤 1 db.fsyncLock() //上同步锁
     2 复制数据文件
     3 db.fsyncUnLock() //解锁
  
      Back Up with mongodump  使用 mongodump进行备份 适合单机,集群备份请使用专业工具
      simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.  快速有效的备份,但不适于大量数据的备份
      mongodump only captures the documents in the database 注意,mongodump只备份文档,对于索引等其他的是不备份的。
      步骤 mongodump --collection myCollection --db test  --out /backup/dir
      具体参数请自行查看 mongodump –help
      恢复备份 mongorestore   <path to backup>
7.运行状态
mongostat captures and returns the counts of database operations by type
mongostat 显示操作(增删改查)的次数

      mongotop tracks and reports the current read and write activity of a MongoDB instance, and reports these statistics on a per collection basis.
      mongotop 对当前的mongodb实例进行跟踪和报告

      Mongodb web 控制台 http://localhost:port  port=mongod的port+1000 默认是28017

      db.serverStatus() from the shell, returns a general overview of the status of the database
      db.serverStatus()可以查看整个mongodb的状态

      db.stats() from the shell, returns a document that addresses storage use and data volumes.
      db.stats() 返回当前数据库的状态

      db.collection.stats() provides statistics that resemble dbStats on the collection level
      db.collection.stats() 返回当前集合的状态
   
      rs.status() returns an overview of your replica sets status
      rs.status() 返回集群的状态

8.索引
格式 db.collection.createIndex( {<key and index type specification>, <options>},{xxx1:xxx2} )
key:要索引的列名 options: 1|-1  顺序还是逆序索引
xxx1:索引参数名   xxx2:索引参数值
对大量数据索引会影响性能,所以一般会实行后台索引 db.collection.createIndex({key:options},{backgroup:true})
对索引进行命名
db.collection.createIndex({key:options},{name:yourName })

复合索引  db.collection.createIndex( { <field1>: <type>, <field2>: <type2>, ... } )
There can be no more than 31 fields in a compound index
复合索引不允许超过31个复合列,item和stock不能同时为数组
举例说明: db.products.createIndex( { "item": 1, "stock": 1 } )
The index will contain references to documents sorted first by the values of the item field and, within each value of the item field, sorted by values of the stock field
首先先按item排序,每个item内部再按stock排序

Text索引  尽量不要使用,很耗时耗内存 
db.reviews.createIndex( { comments: "text" } )
db.reviews.createIndex( { title:”text”,comments: "text" } ) //复合索引
db.reviews.createIndex( { title:”text”,comments: "text" },{weight:{title:2,comments:5}} ) 权重
db.collection.dropIndex()   //删除索引
db.collection.getIndexes()  //获取索引

Hash索引   限制:无法使用复合索引
db.collection.createIndex( { _id: "hashed" } )
db.collection.createIndex( { _id: "hashed" ,name:”hashed”} )  注意,这是错误的

TTL索引    Time To Live
限制:无法对_id使用,不支持复合索引,eventlog不能使capped collection
db.eventlog.createIndex( { name: 1 }, { expireAfterSeconds: 3600 } )

唯一索引
db.members.createIndex( { "user_id": 1 }, { unique: true } )

稀疏索引
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value
稀疏索引只索引包含该索引列的文档,注意document的格式是不固定的。
db.addresses.createIndex( { "xmpp_id": 1 }, { sparse: true } )

集群索引
1.Stop One Secondary                mongod --port 47017
2.Build the Index                    db.records.createIndex( { username: 1 } )
3.Restart the Program mongod         mongod --port 27017 --replSet rs0
第一步,停止mongod,在另一个端口启动单例mongod。
第二步,在其上创建索引。
第三步,停止单例mongod,使用原先的集群配置再次启动。

注意,官网如是说
When building an index on a collection, the database that holds the collection is unavailable for read or write operations until the index build completes
建立索引的时候,mongodb不允许读写直到索引完成。除非加上参数backgroup:true
For replica sets, secondaries will begin building indexes after the primary finishes building the index. In sharded clusters, the mongos will send createIndex() to the primary members of the replica set for each shard, which then replicate to the secondaries after the primary finishes building the index.
Replica建立索引的时候,主节点先建索引,完成后,子节点在建立索引。
在Sharded(分片)集群中,主节点的各个分片先建立索引,完成后,轮到子节点各个分片Ensure that your oplog is large enough to permit the indexing or re-indexing operation to complete without falling too far behind to catch up
集群建立索引的时候,要设置足够大的oplog,否则会报错。
查看索引性能 report使用zipcode索引的性能
db.people.find(
 { name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } ).explain("executionStats")

db.collection.totalIndexSize()  查看索引的大小,物理内存最好大于它

9.存贮引擎
WiredTiger is the default storage engine starting in MongoDB 3.2. It is well-suited for most workloads and is recommended for new deployments. WiredTiger provides a document-level concurrency model, checkpointing, and compression, among other features. In MongoDB Enterprise, WiredTiger also supports Encryption at Rest.
大多数情况下使用wiredTiger引擎有更好的表现,而且它支持文档锁(document-level)

MMAPv1 is the original MongoDB storage engine and is the default storage engine for MongoDB versions before 3.2. It performs well on workloads with high volumes of reads and writes, as well as in-place updates.
Mmapv1引擎在大规模高并发读写方面,性能更好 但还是不建议使用,它的故障恢复功能较弱,只有通过日志和备份恢复。利用多核CPU方面也做的不好。

The In-Memory Storage Engine is available in MongoDB Enterprise. Rather than storing documents on-disk, it retains them in-memory for more predictable data latencies.
In-Memory引擎只有企业版才有,使用内存,速度更快,单对内存要求很高。

WiredTiger引擎
Document Level Concurrency   文档级别的并行性,mmapv1是集合级别的并行性
Snapshots and Checkpoints     快照和检查点能在mongodb cash的时候快速恢复
Journal(write-ahead transaction log) 日志,有助于快速恢复
Compression                 支持对集合和文档的压缩
Memory Use                 60% RAM或者最小1GB  使用率更高,高效用多核CUP

In-Memory引擎
the in-memory storage engine does not maintain any on-disk data, including configuration data, indexes, user credentials, etc.
    所有数据都放内存中,风险很高,适合于特别要求性能,且对宕机后可快速恢复的数据,如省市区信息,这种不经常写入的数据。
    mongod --storageEngine inMemory --dbpath <path>  启用In-Memory引擎
    Warming: The in-memory storage engine does not persist data after process shutdown.
recovery of in-memory data is impossible
    官网有个大大的提醒:内存引擎是不持久化数据的,就是一旦宕机了,就啥也没了。
Memory Use      50% RAM 或者最小1GB
   
GridFS  适用于大于16M的文件,如果小于,官网建议使用document
When to use  什么时候用
If your filesystem limits the number of files in a directory
当文件系统限制文件数量的时候
When you want to access information from portions of large files without having to load whole files into memory
当仅仅使用部分文件的内容时
When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities
希望保证文件同步的时候

chunks stores the binary chunks.  chunks集合存贮文件的二进制块
files stores the file’s metadata.    files集合存贮文件的描述信息

10.安全
创建用户
use reporting
db.createUser(
  {
    user: "reportsUser",
    pwd: "12345678",
    roles: [
       { role: "read", db: "reporting" },   //可以对reporting读
       { role: "read", db: "products" },    //可以对products读
       { role: "read", db: "sales" },       //可以对sales读
       { role: "readWrite", db: "accounts" }  //可以对accounts读写
    ]
  }
)
创建角色
use admin
db.createRole(
   {
     role: "mongostatRole",    //角色名
     privileges: [             //权限
       { resource: { cluster: true }, actions: [ "serverStatus" ] }
     ],
     roles: []    //继承哪些角色
   }
)
分配角色
use reporting
db.grantRolesToUser(
    "reportsUser",
    [
      { role: "read", db: "accounts" }
    ]
)

db.getRole( "read", { showPrivileges: true } ) 查看角色权限
Mongodb内建角色
Read    provides the ability to read data on all non-system collections
读      提供除了系统表之外其他表的读取功能
readWrite  provides all the privileges of the read role and the ability to modify data on all non-system collections
   读写    提供除系统表之外其他表的读写功能
   详细资料请参考 https://docs.mongodb.com/manual/core/security-built-in-roles/
需要注意的一点:
a role can only include privileges that apply to its database and can only inherit from other roles in its database,except for database admin.
意思是,创建的角色只对当前数据库有效,除了在admin数据库中创建的角色。Admin中创建的角色可以对其他数据库进行操作
Ensure that the HTTP status interface, the REST API, and the JSON API are all disabled in production environments to prevent potential data exposure and vulnerability to attackers.
生产环境的时候请确保关闭http,rest api,json api 功能。

使用bing_ip限制访问的ip
关于相关权限处理的方法请参考 https://docs.mongodb.com/manual/reference/security/
11.集群
A replica set in MongoDB is a group of mongod processes that maintain the same data set
The primary node receives all write operations。
replica set 能够集合多个mongod进程为同一份数据服务,且只有主节点才能写。
The secondaries replicate the primary’s oplog and apply the operations to their data sets such that the secondaries’ data sets reflect the primary’s data set
子节点依赖于主节点的日志进行数据同步。
Replica set members send heartbeats (pings) to each other every two seconds. If a heartbeat does not return within 10 seconds, the other members mark the delinquent member as inaccessible
集群中各个节点彼此发送ping消息,每2秒钟一次。如果某个节点10秒内未能应答,则集群就会标识这个节点无法连接。
The purpose of an arbiter is to maintain a quorum in a replica set by responding to heartbeat and election requests by other replica set members
arbiter 的作用是检测集群的健康状况,并进行选举。
If your replica set has an even number of members, add an arbiter to obtain a majority of votes in an election for primary
建议是,当集群数量很多的时候,单独一台机子安装arbiter,专门负责集群的选举。
When a primary does not communicate with the other members of the set for more than 10 seconds, an eligible secondary will hold an election to elect itself the new primary
当子节点链接主节点时间超过10秒,子节点会认为,主节点crash了,然后子节点对集群发出选举,提名自己成为新的主节点。
Primary : receives all write operations.   
Seconderies: replicate operations from the primary to maintain an identical data set
所有写均要经过主节点,所有子节点均是主节点的一个备份。
A replica set can have up to 50 members but only 7 voting members
Replica set 目前只允许最多50个节点,允许最多有7个投票节点。

You can configure a secondary member for a specific purpose
出于各种目的,你可以随意设置子节点。 
Prevent it from becoming a primary in an election, which allows it to reside in a secondary data center or to serve as a cold standby. See Priority 0 Replica Set Members.
使子节点成为一个冷备份,可以设置priority:0,这样,子节点就不能参与选举了
Prevent applications from reading from it, which allows it to run applications that require separation from normal traffic. See Hidden Replica Set Members.
使子节点成为隐藏节点,可以在集群故障的时候快速替换
Keep a running “historical” snapshot for use in recovery from certain errors, such as unintentionally deleted databases. See Delayed Replica Set Members.
使子节点成为延迟备份,可以在集群人为误操作后快速还原到前半小时或一小时的状态,延迟时间可以配置,默认是一小时。

If your deployment requires more than 50 members, you’ll need to use master-slave replication. However, master-slave replication lacks the automatic failover capabilities.
注意:如果数据过多,需要的集群规模很大,超过50个节点,那么,repleca set目前来说,不适合,你需要转换成master-slave模式(最简单的集群,缺乏故障自动恢复能力)

Priority 0 Replica Set Members      设置priority的目的
  A priority 0 member is a secondary that cannot become primary
  A priority 0 member can function as a standby
     in sets with varied hardware or geographic distribution, a priority 0 standby ensures that only qualified members become primary
   Hidden Replica Set Members       设置隐藏节点的目的
     A hidden member maintains a copy of the primary’s data set but is invisible to client applications
     Use hidden members for dedicated tasks such as reporting and backups
   Delayed Replica Set Members       设置延迟节点的目的
     a delayed member’s data set reflects an earlier, or delayed, state of the set
     Must be priority 0 members. Set the priority to 0 to prevent a delayed member from becoming primary.
Should be hidden members. Always prevent applications from seeing and querying delayed members
  
   Oplog      日志
     The oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases.
     记录所有的操作,可以通过它来恢复数据。
     The default oplog size depends on the storage engine:
Engine   Default Size    Lower Bound       Upper Bound
In-Memory Storage Engine 5% of physical memory 50 MB 50 GB
WiredTiger Storage Engine 5% of free disk space 990 MB 50 GB
MMAPv1 Storage Engine 5% of free disk space 990 MB 50 GB
默认大小
引擎        大小         下限        上限
In-Memory   5%内存       50M        50G
      wiredTiger   5%剩余磁盘   990M       50G
      MMAPv1    5%剩余磁盘   990M       50G

  Master-Slave   主从集群,操作简单,没有故障自动恢复功能,不做重点讲解
mongod --master --dbpath /data/masterdb/ --oplogSize 1024   启动master
mongod --slave --source <masterhostname><:<port>> --dbpath /data/slavedb/  启动slave
注意,日志一定要足够大,原因在于,master节点接受写要求,数据同时记录到日志中,然后,master节点把日志分发给各个slave节点,slave节点执行日志的内容,以达到数据同步。一旦大规模写,而日志容量太小就有可能造成,主从不同步,数据不一致的情况。
rs.printReplicationInfo(),rs.printSlaveReplicationInfo()  查看主节点状态,从节点状态
更多方法请参考https://docs.mongodb.com/manual/reference/replication/

mongod --replSet "rs0"
rs.initiate()   //初始化
rs.conf()     //查看配置
rs.add("mongodb1.example.net")   //添加节点
rs.status()   //查看状态
以上所有都必须在主节点上进行

修改节点优先级
cfg = rs.conf()
cfg.members[2].priority = 0.5   //修改节点的优先级
rs.reconfig(cfg)

添加arbiter节点
rs.addArb("m1.example.net:30000")

可用参数
rs.add({_id: 1, host: "mongodb3.example.net:27017", priority: 0, hidden: true})
rs.add({_id: 1, host: "mongodb3.example.net:27017", priority: 0, hidden: true, slaveDelay:3600})
{
  _id: <string>,
  version: <int>,
  protocolVersion: <number>,
  members: [
    {
      _id: <int>,
      host: <string>,
      arbiterOnly: <boolean>,
      buildIndexes: <boolean>,
      hidden: <boolean>,
      priority: <number>,
      tags: <document>,
      slaveDelay: <int>,
      votes: <number>
    },
    ...
  ],
  settings: {
    chainingAllowed : <boolean>,
    heartbeatIntervalMillis : <int>,
    heartbeatTimeoutSecs: <int>,
    electionTimeoutMillis : <int>,
    getLastErrorModes : <document>,
    getLastErrorDefaults : <document>
  }
}

移除已有节点
rs.remove("mongod3.example.net:27017")
对应节点关闭mongod进程 db.shutdownServer()

12.分片
Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.
Sharding就是将大规模数据分片,每个分片各自占用一个mongod进程,方便处理。ar

Sharding系统包含以下几个进程
Shard: Each shard contains a subset of the sharded data
      每个分片包含整个数据的一小部分
Mongos: The mongos acts as a query router
      起到路由的作用
Config Server: Config servers store metadata and configuration settings for the cluster
      存贮分片集群的配置信息

分片好处:分布式读写,大规模存贮,高可用性

For queries that include the shard key or the prefix of a compound shard key, mongos can target the query at a specific shard or set of shards
对于前缀带shard key的复合查询可以快速定位到某个分片,无需扫描整个数据。
这里点出了一个性能优化要点:分片集群的增删改查尽量使用shard key
For queries that do not include the shard key or the prefix of a compound shard key, mongos performs a broadcast operation, querying all shards in the sharded cluster
对于不包含shard key的查询,就需要扫描所有分片,然后再聚合起来,影响速度。

In production environments, individual shards should be deployed as replica sets
Mongodb3.2版本提供shard的replica,在商用上,应该设置分片的备份。

You cannot change the shard key after sharding, nor can you unshard a sharded collection
注意:以key=x分片之后就不能在修改为key=y了,集群分片之后也无法还原为未分片前的状态了。

分片策略    优点                     缺点                   适用
  Hash      数据均匀分布             顺序查询慢             基本适用
  Range     可针对某个阶段分片       数据容易出现头重脚轻   按阶段查询


                               Hash sharp key


                              Range sharp key

Sharp key 限制
You cannot select a different shard key for that collection.
一旦选定sharp key就不能再更改
You cannot update the values of the shard key fields.
不允许更新sharp key
You cannot shard a collection that has unique indexes on other fields.
有除sharp key列外其他列的唯一索引的集合不能分片
You cannot create unique indexes on other fields for a sharded collection
分片集群不能创建其他列的唯一索引

     sh.shardCollection( "database.collection", { <field> : "hashed" } ) 创建hash sharp key

     创建Hash分片集群
1.Create the Config Server Replica Set
1)Start each member of the config server replica set.
           mongod --configsvr --replSet <setname> --dbpath <path>
        2)Connect to one of the config servers
           mongo --host <hostname> --port <port>
        3)initiates the replica set
           rs.initiate(
              {
                 _id: "<replSetName>",
                 configsvr: true,           注意一定要有这个    
                 members: [
                   { _id : 0, host : "cfg1.example.net:27017" },
                   { _id : 1, host : "cfg2.example.net:27017" },
                   { _id : 2, host : "cfg3.example.net:27017" }
                 ]
               }
)
2.Create the Shard Replica Sets
1)Start each member of the shard replica set.
mongod --shardsvr --replSet <replSetname>
2)Connect to a member of the shard replica set
mongo --host <hostname> --port <port>
3)Initiate the replica set.
            rs.initiate(
              {
                 _id: "<replSetName>",
                 configsvr: true,
                 members: [
                   { _id : 0, host : "cfg1.example.net:27017" },
                   { _id : 1, host : "cfg2.example.net:27017" },
                   { _id : 2, host : "cfg3.example.net:27017" }
                 ]
               }
)
3.Connect a mongos to the Sharded Cluster
1)Connect a mongos to the cluster
mongos --configdb <configReplSetName>/host1:port,host2:port,……
2)Connect to the mongos
mongo --host <hostname> --port <port> 
4.Add Shards to the Cluster
sh.addShard( "<replSetName>/host:port ")
5.Enable Sharding for a Database
sh.enableSharding("<database>")
6.Shard a Collection using Hashed Sharding
If the collection already contains data, you must create a Hashed Indexes on the shard key using the db.collection.createIndex() method before using shardCollection().

sh.shardCollection("<database>.<collection>", { <key> : <direction> } )

创建Range分片集群
   步骤和创建Hash分片集群一致,除了第6步,Range分片创建的是普通索引,而Hash分片创建的是hash索引

相关分片命令参考https://docs.mongodb.com/manual/reference/sharding/
13.性能测试
测试前期准备已经完毕,由于网络的原因,目前机子不可用,等网络通了,就可以进一步完善了。

MongoDB安装部署 Shell 环境基本操作 下载 一、下载MongoDB 打开官网下载地址:https://www.mongodb.com/download-center#community 文档地址:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 分别对应4个平台,windows、Linux、OSX和Solaris 安装 官... 阅读详情

相关推荐

BurpSuite+安卓模拟器双保险:避开真机抓包限制的完整配置指南

本文详细介绍了如何通过BurpSuite与安卓模拟器(如雷电、夜神)协同工作,绕过真机抓包限制的完整配置指南。从环境准备、网络拓扑配置到证书部署和流量分析,提供了一套高效的解决方案,帮助安全测试人员稳定捕获手机数据包,提升测试效率。

Apple的专栏 439

MongoDB之GridFS

【MongoDB是什么?】 MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,

yangyatou1991的专栏 1万+

SIMetrix教程-001.SIMetrix软件简介与安装

  由于某些原因需要用到SIMetrix仿真软件,然而网上的资料并不是特别多,故在此记录一下这款仿真软件的学习过程,也给有需要的人提供一些参考,免走弯路。如果使用过Pspice或者LTspice软件,学习SIMetrix会很容易入门,仿真软件都是大同小异的,会一样后都可以快速入门其他软件。   SIMetrix是一款以SPICE模型为基础的仿真软件,集成了SIMetrix和SIMPLIS两款内核,为工程师提供了可靠的、易于使用的、并完美结合了速度与精度的电路仿真方式。官方的话不多说,和LTspice一样,优

gaoyong_wang的博客 2万+

「SequoiaDB巨杉数据库」SQL语法-drop collection

用于删除集合空间中的集合。 语法 drop collection <cs_name>.<cl_name> 参数 参数名 参数类型 描述 是否必填 cs_name string 集合空间名。 是 cl_name string 集合名。 是 Note: 集合空间必须在数据库中存在。 集合必须在该集合空间中存在。 返回值 无。 示例 删除集合空间 sample 中的集合 employee。

芬达的博客 349

system_stm32f10x.c文件分析

配置系统时钟(包括系统时钟源、锁相环(PLL)倍频系数、高级高性能总线(AHB)/ 高级外设总线(APBx)预分频器以及闪存相关设置)。该函数在系统复位后的启动阶段被调用,且调用时机早于跳转到主程序之前。

Jia shuheng的博客 603

uniapp+openlayers集成天地图实战:renderjs通信陷阱与性能优化避坑指南

本文深入探讨了uniapp与openlayers集成天地图时的renderjs通信陷阱与性能优化策略。通过实战案例解析了renderjs的特殊通信机制、地图实例生命周期管理、图层懒加载等关键技巧,帮助开发者避免常见坑点,提升应用流畅度。特别针对多语言切换、热重载偏移等疑难问题提供了解决方案,是uniapp+openlayers开发者的实用避坑指南。

weixin_30872157的博客 363

Python MongoDB 删除集合(Drop Collection)

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。MongoDB 是目前最流行的 NoSQL 数据库之一,MongoDB使用了BSON(类似 JSON)这种结构来存储数据和网络数据交换。本文主要介绍Python MongoDB 删除集合(Drop Collection)。 原文地址:Python MongoDB 删除集合(

亮亮的专栏 1095

基于Multisim的智能交通灯控制系统仿真与实现(含动态闪烁功能)

本文详细介绍了基于Multisim的智能交通灯控制系统仿真与实现,重点解析了动态闪烁功能的电路设计。通过555定时器、74LS192计数器和JK触发器等核心模块,实现了主干道与支干道的交替通行及黄灯1Hz闪烁功能,为交通灯控制系统设计提供了完整的仿真方案。

weixin_29174013的博客 427

mongo 的安装与使用

MongoDB安装 官方下载地址 RPM包列表 包名称 命令集 说明 mongodb-org-server-4.0.4-1.el6.x86_64.rpm mongod 守护进程 mongodb-org-mongos-4.0.4-1.el6.x86_64.rpm mongos 分片群集的控制器和查询路由器 mongodb-org-shell-4.0.4-1.el6.x86_64.rpm mongo 交互式的MongoDB Shell mongodb-org-tools-4.0.4

Valiant 的博客 415

BUCK电路的EMI优化设计案例

10μH的电感以及SW铺铜面积都比较大,在保证电流能力的情况下,切掉部分SW面积,换用更小体积的小感值电感。最终结果如图14.38所示,优化SW面积和电感后,提升了10dB左右,基本满足了class 5的限值要求。如图14.36,为一个BUCK电路的LED驱动板,从原理图来看,绿色的滤波器输入地和黄色的功率地通过输入电容隔开,这样高频电流环路的噪声电流会先经过输入的陶瓷电容,但是实际的PCB布局中,可以看到输入地和高频电流环路重合,噪声可以直接耦合到输入环路上。图 14.36 一个BUCK电路的布局。

qq_43416206的博客 525

华为OD机试E卷 -猜字谜(Java & Python& JS & C++ & C )

小王设计了一个简单的猜字谜游戏,游戏的谜面是一个错误的单词,比如nesw,玩家需要猜出谜底库中正确的单词。猜中的要求如下:请你写一个程序帮忙在谜底库中找到正确的谜底。谜面是多个单词,都需要找到对应的谜底,如果找不到的话,返回”not found”

算法大师! 3488

Node.js 操作 MongoDB

本文介绍了如何使用Node.js操作MongoDB数据库。主要内容包括:MongoDB作为文档型数据库的特点和功能;安装启动方法;通过Mongoose模块建立Node.js连接;定义数据模型;实现CRUD操作(增删改查);推荐可视化工具;以及项目结构示例。文章提供了完整的代码示例,帮助开发者快速掌握Node.js与MongoDB的交互操作,适合需要实现数据持久化的Node.js开发者参考学习。

2401_83194362的博客 1085

应用商店评论分析实战:Python逆向工程破解Google Play加密接口与NLP建模

在移动互联网时代,应用商店中的用户评论成为了开发者优化应用、提升用户体验的重要依据。然而,Google Play等应用商店对评论数据进行了加密处理,直接爬取难度较大。本文将介绍如何使用Python逆向工程破解Google Play的加密接口,实现对应用商店评论的实时采集,并结合NLP(自然语言处理)技术进行情感分析和主题建模,为开发者提供有价值的数据支持。

u014481728的博客 1350

Node.js——操作MongoDB

由于MongoDB数据库在JavaScript脚本环境中支持BSON对象(JSON对象的二进制形式)的存取,因此对于数据的存取的效率是非常高的。在MongoDB数据库中,将每一条等待插入的数据记录存储在内存中,因此,该数据库是一种非阻塞型数据库,在需要记载大量日志数据、实时测量数据或实时统计数据时,该数据库可以达到令人满意的效果。由于MongoDB数据库支持在查询语句内部使用JavaScript函数,也大大加强了它读取数据的能力。数据类型说明数据使用示例Array数组Boolean。

吴声子夜歌的博客 432

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

双系统重装Ubuntu20.04及系统基本配置

这里写自定义目录标题前言Ubuntu20.04换源科学上网(客户端配置)安装chrome浏览器安装OneNote安装Wechat QQ Dingtalk参考 前言 笔者从2019年9月份去长沙智能驾驶研究院开始接触ubuntu18.04,在2020年4月份做本科毕业设计的时候也来来回回重装过几次ubuntu,始终没有把自己的重装过程记录下来(其实就是因为懒)。2020/12/12号,我在未关闭chrome浏览器,同时电脑上插着ubuntu18.04的启动U盘的情况下关闭双系统的ubuntu18.04,结果再

Bill_gatesofboss的博客 1万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

低版本Ubuntu升级为高版本libc6

Ubuntu的libc6升级2.32以上

TaviaHong的博客 4万+

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Ubuntu下安装docker方法详解

从19年开始,在华为的一次面试中第一次听到docker,后来因为找工作和入职开发任务繁忙没有太多的去研究这一块,直到最近我们公司的软件供应商交付的产品是用docker作为承载来开发,这一次必须来研究研究如何部署docker了。 部署之前的准备 准备好相应的软件 测试 一、部署之前的准备 部署之前必须得说一句,我之前用的是window7系统32位,但是拿到官方文档一看才知道,用现在...

Seagull的专栏 1278

Ubuntu安装Nginx 问题以及解决办法

Ubuntu安装Nginx 问题以及解决办法

wangchaoqi1985的博客 373

Python-pip 安装失败问题解决

一、问题描述root@ubuntu:/home/chao# apt-get install python-pip 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 有一些软件包无法被安装。如果您用的是 unstable 发行版,这也许是 因为系统无法达到您要求的状态造成的。该版本中可能会有一些您需要的软件 包尚未被创建或是它们已被从

微寒的技术博客 3万+

CentOS7安装MongoDB3.4

全文转自davide_tian的[置顶] CentOS7安装MongoDB3.4感谢davide_tian,让我能够在一次又一次失败之后找到一种靠谱的安装方式。简介 MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询

喜爱设计架构,追求项目可控。 1263

不同操作系统中安装MongoDB的详细步骤

安装完成后,你可以通过连接到MongoDB数据库并执行一些简单的操作来验证安装是否成功。例如,在命令提示符或终端中运行。服务已经启动),进入MongoDB的shell环境,尝试执行一些数据库操作,如。

867

【docker】安装制作入坑笔记

背景 天天听群里那么多人吹docker,自己又没用过,早就想试一下了。 安装CentOS 为了节省买服务器的钱,我通过虚拟机做一个centos8的系统。 vmware虚拟机下载 链接:https://pan.baidu.com/s/1EOl2k4chiPx20cuBvijBaQ 提取码:tsfl centos8下载 选择简易安装,然后会出现pane is dead,估计是因为它以为是cent...

不忘初心,坚定道路! 406

【数据库】MongoDB数据库详解

详细介绍MongoDB数据库的基本知识,安装方法,基本操作,

weixin_48814356的博客 7730

ubuntu 安装 mongodb 笔记记录

安装过程没有出现错误信息,可以认为 MongoDB 服务器已经成功安装到你的系统上。- **用户和组创建**:系统为 MongoDB 创建了专用的用户 `mongodb` 和组 `mongodb`。- **安装过程顺利**:从日志来看,`mongodb-org-server` 包已经成功解包并安装。- **触发器处理**:安装完成后,系统处理了 `man-db` 的触发器,这通常是为了更新手册页。如果服务启动成功且状态正常,说明安装完全成功。补充 libssl1.1。再更新缺少的库:又錉了。

weixin_42771529的博客 1632
上一篇: Spring 框架(二)
下一篇: nodejs项目windows下开机自启动
github_39294367
博客等级 码龄9年 33粉丝 156原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值