本质上是JavaScript控制台

不要设置Windows service
不要install mongodb compass,在官网下载
7以上版本要下shell
在cmd里输入mongo
show dbs
1.建立并进入
use <数据库名字>
use <数据库名字>
quit()
db.stats()
db.getCollectionNames()
dp.dropDatabase()
db.blog<集合名称>.insert({title:"welcome",author:"zhangsan",Context:"xxxxxxxxx"}
db.blog.find():查询所有
db.blog.find(
example:db.blog.find({author:'sb'}).pretty()
pretty():有缩进
limit(int num):查前几个数据
skip(int num):跳过前几个数据


只更新查到的第一个文档,如果一次修改全部,加上第三个{multi:true}
db.blog.update({title:'welcome'},{$set:{title:'welcome!!'}})

自加:db.blog.update({title:'today'},{$inc:{count:1}})
重命名:db.blog.update({title:'today'},{$rename:{coment<旧名字>:'comment'<新名字>}})
全改(multi的使用):db.blog.update({author:'jack'},{$set:{author:'jacky'}},{multi:true})
插入:db.blog.update({ author: "smith", title: "smith blog" },{ $set: { author: "smith", title: "smith blog" } },{ upsert: true })
删除数据:db.blog.update({title:'today'},{$unset:{count:{}}})
$pop:删除第一个或最后一个
$push:插入,插入多个元素:{$push:{},{}}
$pull:删除元素
db.blog.remove(<查询条件>)
db.blog.drop():删除所有文档
1.使用insert方法向数据库的blog集合中插入5个文档。
db.blog.insert(
[
{author:"tom",title:"1st blog",content:"It\'s my first blog",count:10},
{author:"jack",title:"hello",content:"hello world!",stats:{flag:0, type:2},count:6},
{author:"tom",title:"2nd blog",content:"today is a sunny day.",count:4},
{author:"jack",title:"noSQL",content:"MongoDB is a noSQL database.",count:2,comments:[{user:"tom",comment:"hello"}]},
{author:"tom",title:"3rd blog",content:"have a nice day.",hide:true}
]
)
2.根据以下的要求完成blog集合中文档的更新(update)
(1)将作者为tom,标题为1st blog的文档的count值设置为20。
db.blog.update( {author:'tom',title:'1st blog'}, {$set:{count:20}})
(2)将作者为tom,标题为2nd blog的文档的count值加1。
db.blog.update({author:'tom',title:'2nd blog'},{$inc:{count:1}})
(3)将作者为jack的文档的作者修改为jacky(multi选项)。
db.blog.update({author:'jack'},{$set:{author:'jacky'}},{multi:true})
(4)将作者为tom,标题为3rd blog的文档的hide字段删除;并新增count字段,值设置为1。
db.blog.update({title: '3rd blog',author: 'tom'},{$unset: { hide: {} },$set: { count: 1 }})
(5)利用upsert选项,更新的同时插入一个作者为smith,标题为smith blog的文档。
db.blog.update({ author: "smith", title: "smith blog" },{ $set: { author: "smith", title: "smith blog" } },{ upsert: true })
(6)将作者为jacky,标题为hello的文档的stats字段中的flag字段设置为1。
db.blog.update({author:'jacky', title:'hello'}, {$set: {'stats.flag': 1}})
(7)数组更新:将作者为jacky,标题为noSQL的文档的评论(comments字段)新增一个用户名为smith的评论;然后删除smith的评论。
db.blog.update({author: 'jacky',title: 'noSQL',},{$push: {comments: {user: 'smith'}}})
db.blog.update({author: 'jacky',title: 'noSQL',},{$pull: {comments: { user: 'smith' }}})
3.删除(delete)文档
(1)删除作者为smith的文档
db.blog.remove({author:"smith"})

The primary node receives all write operations. A replica set can have only one primary capable of confirming writes.主节点接收所有写操作。一个复制集只能有一个能够确认写操作的主节点。
The secondaries replicate the primary’s operation log(oplog) and apply the operations to their data sets.次要服务器复制主服务器的操作日志(oplog),并将操作应用于它们的数据集。
If the primary is unavailable, an eligible secondary will hold an election to elect itself the new primary.如果初选不可用,一个合格的候补将举行选举,选出自己作为新的初选。
A replica set in MongoDB is a group of mongod processes(MongoDB instance) that maintain the same data set.MongoDB中的副本集是一组维护相同数据集的mongod进程(MongoDB实例)。
Replica sets provide redundancy and high availability.副本集提供冗余和高可用性。
A replica set contains several data nodes and optionally one arbiter node. Of the data nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.副本集包含多个数据节点和一个可选的仲裁节点。在数据节点中,一个且只有一个成员被视为主节点,而其他节点被视为辅助节点。
The primary node receives all write operations.
A replica set can have only one primary capable of confirming writes. The secondaries
replicate the primary' s operation log(oplog) and apply the operations to their data sets.
If the primary is unavailable, an eligible secondary will hold an election to elect itself the new primary.

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.分片是一种跨多台机器分发数据的方法。MongoDB使用分片来支持非常大的数据集和高吞吐量操作的部署。
vertical and horizontal scaling Horizontal Scaling involves dividing the system dataset and load over multiple servers, adding additional servers to increase capacity as required.横向扩展包括将系统数据集和负载划分到多个服务器上,根据需要添加额外的服务器来增加容量。
MongoDB supports horizontal scaling through sharding.MongoDB支持通过分片进行水平扩展。
A MongoDB sharded cluster consists of the following components:MongoDB分片集群由以下组件组成:
1.shard: Each shard contains a subset of the sharded data. Each shard can be deployed as a replica set.
MongoDB shards data at the collection level, distributing the collection data across the shards in the cluster.
2.mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster.
3.config servers: Config servers store metadata and configuration settings for the cluster.
Shard Keys MongoDB uses the shard key to distribute the collection's documents across shards.Shard Keys MongoDB使用Shard key将集合的文档分布到不同的Shard上。
Chunks MongoDB partitions sharded data into chunks.chunk MongoDB将数据分片成块。