mongodb笔记

1. 32位的MongoDB数据量受限于2.5GB,The reason for this is that the MongoDB storage engine uses memory-mapped files for performance.
   所以推荐使用64-bit的MongoDB。
2. fsync时可以选择加上lock选项,此时所有的写操作将被阻塞,读操作仍然可用,但若此时有写操作,则读操作也被阻塞,因为用的是读写锁。使用unlock命令进行解锁。
3. 未使用--journal选项时,使用getlasterror命令+fsync选项获取最后一次命令执行的结果,则会强制将内存中的数据同步到data files,并返回是否成功结果,可用于同步执行更新操作(safe mode),确保更新操作成功执行。
4. fsync会将内存中的数据写入到物理data files中,若开启--journal选项,则fsync会等待下一次group commit将内存中数据写入到journal文件,而非data files,然后返回。
5. 开启--journal选项时,数据先写入到内存,然后间隔地执行group commit,批量将内存中的数据写入到journal文件。一般在100ms的间隔。
6. 开启--journal选项时,内存数据会间隙性地批量提交到journal,并且也会更大间隔地提交到data files,一旦数据进入data files,那么对应的journal文件会被滚动掉(rotate out)。
7. getlasterror没有带fsync选项时,只要写入到内存即返回成功,否则会迫使内存中数据写入到data files或journal文件,然后返回成功或失败。
8. 有些driver实现了安全模式(safe mode),即更新需要实时返回是否成功的结果。
9. 新版本中的索引创建支持在后台进行,并不影响更新数据的速度。
10.db.currentOp()当前正在执行的操作。
11.注意对于mongodb来说BSON结构中的属性是有顺序的。(如联合索引是{a, b},则查询时以{b, a}顺序是无法使用上该联合索引的)。
12.对于组合索引匹配条件是查询中全部"列"出现,或组合索引中第一个"列"出现。
13.sparse index不会包含不存在该field的document,且该索引只能有一个field。如:该索引的field为a,那么find({a : null})不包含a字段的document不会返回,而find({b : null})会返回不包含b字段的document。
14.物理位置的索引,在1.9.1之前精确匹配只支持物理坐标数据是以数组形式存储时。当前版本只支持一个collection一个物理索引。
15.在修改某条document记录时,如果大小有变动,由于mongodb底层实现会预留空间,所以不需要从新移动数据。
16.何时建立索引:需要查询的field建立索引,需要排序的field建立索引,若是联合索引,则sort field排在最后。
Note that adding an index slows writes to a collection, but not reads. Use lots of indexes for collections with a high read / write ratio (assuming one does not mind the storage overage). For collections with more writes than reads, indexes are very expensive.
17.  db.things.group(
          {
              keyf : function(obj) {return {w : obj.x + obj.j};},
              reduce : function(obj, prev) {prev.csum += obj.j;},
              initial : {csum : 0}
           }
       )
关于finalize: http://kylebanker.com/blog/2009/11/mongodb-advanced-grouping/
18. 获取更新操作的结果,db.runCommand("getlasterror")
{
        "updatedExisting" : true,//为更新操作,且有document被更新
        "n" : 39, //更新document数
        "connectionId" : 22,
        "err" : null,
        "ok" : 1
}
{
        "updatedExisting" : false, //为更新操作,但没有document被更新
        "upserted" : ObjectId("4dbd73b17479e34a59c101a7"),//若upsert为insert结果
        "n" : 1,
        "connectionId" : 22,
        "err" : null,
        "ok" : 1
}
19. example.js
       db = db.getSisterDB("test");
       db.things.update({j : 32}, {$set : {x : 4000}});
20. 对于大数据量的操作可以使用db.eval()在服务器端执行逻辑,以免传输大量数据到客户端所带来的性能瓶颈。
       db.eval() is used to evaluate a function (written in JavaScript) at the database server.
21. 一个时刻服务端对于javascript的执行仅有单个线程。
22. mongod --fork --auth --logpath=/data/db-log/mongo.log --logappend --journal
       使用journal启动选项来进行crash时的自动恢复,不行还可以使用mongod --repair进行修复
       使用journal时如果需要预先分配硬盘空间,那么会生成3G的prealloc.0[1,2]文件,可以连续空间写,加快速度,真正写journal 时,会变成j._0形式,退出时又恢复为prealloc形式,所以为加快启动速度,可以先生成好这3个文件,然后拷贝到该目录即可立即启动
23. 可以使用logrotate来滚动mongod的日志
       refer: http://groups.google.com/group/mongodb-user/browse_thread/thread/2b014f27697387f9
                 http://notes.alexdong.com/logrotate-settings-for-mongodb(use web proxy)
24. Can I use the journaling feature to perform safe hot backups?
       Not yet, as the journal files are rotated out (unlinked) after data is safely in the data files.
25. Unix上的推荐额外配置
    a. turn off atime
       备份/etc/fstab, 编辑fstab,在需要关闭atime属性的分区option上添加,noatime,nodiratime
    b. Set file descriptor limit
       vi /etc/security/limits.conf,添加*    soft    nofile     65536
    c. Do not use large VM pages with Linux
    d. better to use ext4, xfs file system
26. 可以使用rs.stepDown(120)使当前primary放弃primary权利,在120秒后如果没有人接管primary,则自己从新申请成为primary
       rs.freeze(120)使当前node冻结成为primary的权利120s
       可以使用上述两个命令强制使某个node成为primary
27. 源码编译安装
       http://www.mongodb.org/display/DOCS/Building+for+Linux
       r1.8.1bug修复https://github.com/mongodb/mongo/commit/831eee02a5564354c600535ffa14803bcf27534d
       https://jira.mongodb.org/browse/SERVER/fixforversion/10263#atl_token=ASGJ-NC06-6R0X-EKH7%7C32faddb7f89fc5c30c68e1c0b963d9b5998d01f3%7Clout&selectedTab=com.atlassian.jira.plugin.system.project%3Aversion-summary-panel
28. replica set中的启动命令+参数,较优
    mongod --replSet foo --port 27017 --dbpath /data/r0 --rest --fork --logpath=/tmp/mongod-27017.log --logappend --directoryperdb
    最好设置directoryperdb,这样硬盘空间不足,需要创建新DB时,可以挂载新的硬盘卷到相应DB的目录,这样新的DB文件就存放在新的硬盘卷上。
    mongod --replSet foo --port 27018 --dbpath /data/r1 --rest --fork --logpath=/tmp/mongod-27018.log --logappend --journal --directoryperdb
    对于primary最好不要使用journal选项,提高系统的性能,如果db crash可以使用secondary上文件进行替换恢复,而secondary可以使用该选项,方便crash自动恢复。
29. 缩短磁盘刷新间隔 --syndelay
    60s(default) => 15~30s

猜你喜欢

转载自xiangxingchina.iteye.com/blog/1330634