固定集合值得是事先创建而且大小固定的集合
2 固定集合的特征:固定集合很像环形队列,如果空间不足,最早文档就会被删除,为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制。
3 创建固定集合使用命令:
db.createCollection(“collectionName”,{capped:true,size:100000,max:100});
size:指定集合大小,单位为KB,max指定文档的数量
当指定文档数量上限时,必须同时指定大小。淘汰机制只有在容量还没有满时才会依据文档数量来工作。要是容量满了,淘汰机制依据容量来工作。
4 创建一个集合:
>db.createCollection("cap1",{capped:true,size:1000,max:100});
{ "ok" : 1 }
>
5 插入数据
> for(var i=1;i<=100;i++){
...db.cap1.insert({name:"dongxue",age:i});
... }
WriteResult({ "nInserted" : 1 })
> db.cap1.find().count();
53 (大小之所以是53是因为大小超过了1000)
6 固定集合的应用场景:聊天记录,日志信息
淘汰机制:当满足size指定集合大小,不能再继续往固定集合中加数据。
固定集合的容量优先
当文档达到100条时,再添加的时候会替换先前的
7 备份与导入导出。
MongoDB提供了备份和回复的功能,分别是MongoDB下载目录下的mongodump.exe和mongorestore.exe文件。
备份数据使用下面的命令:
mongodump –h dbhost –d dbname –o dbdirectory
-h:MonDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017,当然该目录需要提前创建,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。
mongodump -h localhost:27017 -d toto -of:/beifeng
-h:用来指定要输出的数据库所在的ip地址和端口号
-d: 指定要备份的数据库
-o: 表示要备份到的文件目录
执行后的效果图:
vcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="border: none; max-width: 100%;" width="554" />
7 另外启动一个mongodb的客户端,目的是将数据库中toto数据库删掉
| C:\Users\to-to>mongo MongoDB shell version: 2.6.4 connecting to: test > use toto; switched to db toto > db.help(); DB methods: db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.r unCommand(...) ] 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.createUser(userDocument) db.currentOp() displays currently executing operations in the db db.dropDatabase() db.eva l(func, args) run code server-side 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.getCollectionNames() db.getLastError() - just returns the err msg string db.getLastErrorObj() - return full status object 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, inherit ed 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()
|