mongo小结(>=2.2)

1、存储模式:面向集合存储,模式自由; GridFS大文件存储(16M)

2、容灾类型:主从复制(Replication)、Replica Set(自动选取主节点)、Sharding + Replica Set。

说明:mongo的Replication采用日志+批量更新方式,效率比直接更新高,Replication更新期间,从节点读性能受影响(锁机制导致)。

3、支持CRUD 和 Fast In-Place Updates(文档内更新)。

说明:Fast In-Place Updates支持快速更新文档自身内容,对于不影响文档大小的更新,效率比覆盖更新整个文档效率高。为了支持改变文档大小的文档内更新,mongo内部采用padding,即采用比文档略大的空间存储文档,当更新导致文档大小改变时,如改变大小不超过padding大小,就地更新,无须另外存储。padding比例由mongo自身决定。

4、读写锁,写锁优先

说明:mongo的锁机制对于解释mongo运行效率很重要。常见需要写锁的操作:insert、update、remove、eval(执行命令或脚本)、createIndex(创建索引需锁定整个collection,数据大时,相当耗时)、replication、TTL的定期删除。

5、存储机制:mmap file + 内存索引。完全由OS处理缓存。磁盘空间预分配(默认2G)。

说明:mongo将内存管理和缓存都交给OS。内存优先让给索引使用。当索引大于内存大小时,将影响各种操作。当"工作集"(热门数据)+索引 大于 内存时,查询性能受影响。

6、集合类型:普通集合、TTL Collection(淘汰过期数据)、 Capped Collection(定长集合,FIFO)

7、同步:拷贝集合数据+日志同步

8、相对丰富的运维工具和shell客户端

使用示例

// this header should be first to ensure that it includes cleanly in any context
#include "mongo/client/dbclient.h"#include <iostream>
#include <vector>
#include <string>#ifndef verify
#  define verify(x) MONGO_verify(x)
#endifusing namespace std;
using namespace mongo;int connect_mongo(DBClientConnection* conn, const string& ip, const string& port)
{ try{if( conn->getServerAddress().empty() ){   // not initstring errmsg;if( ! conn->connect( ip  + ":" + port , errmsg ) ){printf("FAIL connect mongo, %s", errmsg.c_str());return -1;}}if (conn->isFailed()) {string errmsg;if ( ! conn->connect( ip  + ":" + port , errmsg ) ) {printf("FAIL connect mongo, %s", errmsg.c_str());return -1;}}}catch( DBException& e ) {printf("MONGO Exception(connect): %s", e.what());return -1;}catch ( std::exception& e ) {  printf("MONGO Exception(connect): %s", e.what());return -1;}catch( ... ){printf("MONGO Exception(connect): NULL");return -1;}return 0;
}int set_mongo(DBClientConnection* conn, const string& dbname, const string& id, const string& value)
{try{mongo::BSONObjBuilder b;long long curtime = time(NULL);Date_t date( (curtime - curtime % (3600*24) + ( random() % 6 + 2) * 3600) * 1000 ); // limit ttl deleting time to 2:00-8:00.
b.append( "_id", id );b.append( "value", value);b.append( "ts", date);//cout<< b.obj() << endl;conn->update( dbname , BSONObjBuilder().append( "_id" , id ).obj(), b.obj(), true );}catch( DBException& e ) {printf("MONGO Exception(set): %s", e.what());return -1;}catch ( std::exception& e ) {  printf("MONGO Exception(set): %s", e.what());return -1;}catch( ... ){printf("MONGO Exception(set): NULL");return -1;}return 0;
}int get_mongo(DBClientConnection* conn, const string& dbname, const string& id, string& value)
{try{BSONObj obj = conn->findOne( dbname, BSONObjBuilder().append( "_id" , id ).obj()); if( !obj.isEmpty() ){value = obj.getStringField("value");}}catch( DBException& e ) {printf("MONGO Exception(get): %s", e.what());return -1;}catch ( std::exception& e ) {  printf("MONGO Exception(get): %s", e.what());return -1;}catch( ... ){printf("MONGO Exception(get): NULL");return -1;}return 0;
}int mget_mongo(DBClientConnection* conn, const string& dbname, const vector<string>& ids, vector<string>& values)
{try{mongo::BSONObjBuilder b;b.append( "$in",  ids);auto_ptr<DBClientCursor> cursor = conn->query( dbname, BSON( "_id" << b.obj() ));while ( cursor->get() && cursor->more() ) {BSONObj obj = cursor->next();if( !obj.isEmpty() ){values.push_back(obj.getStringField("value"));}}}catch( DBException& e ) {printf("MONGO Exception(get): %s", e.what());return -1;}catch ( std::exception& e ) {  printf("MONGO Exception(get): %s", e.what());return -1;}catch( ... ){printf("MONGO Exception(get): NULL");return -1;}return 0;
}int del_mongo(DBClientConnection* conn, const string& dbname, const string& id)
{try{conn->remove( dbname, BSONObjBuilder().append( "_id" , id ).obj()); }catch( DBException& e ) {printf("MONGO Exception(del): %s", e.what());return -1;}catch ( std::exception& e ) {  printf("MONGO Exception(del): %s", e.what());return -1;}catch( ... ){printf("MONGO Exception(del): NULL");return -1;}return 0;
}void Print(DBClientConnection& conn, const string& dbname)
{auto_ptr<DBClientCursor> cursor = conn.query( dbname , BSONObj() );int count = 0;while ( cursor->more() ) {count++;BSONObj obj = cursor->next();std::cout<< obj << std::endl;}
}int main( int argc, const char **argv )
{const char *port = "27000";string host( "127.0.0.1" );if ( argc < 3 ) {std::cout << argv[0] << " dbname host [port]" << endl;return EXIT_FAILURE;}const char* dbname = argv[1];if( argc >= 3){host = string(argv[2]);}if( argc >= 4){port = argv[3];}{DBClientConnection conn( false , 0 , 2 );if( connect_mongo(&conn, host, port) != 0){exit(-1);}string id = "test123";string value = "test456";del_mongo(&conn, dbname, id);set_mongo(&conn, dbname, id, value);string ret_val;get_mongo(&conn, dbname, id, ret_val);if( value != ret_val){cout<<"TEST FAIL: " << value << " : " << ret_val <<endl;}}cout << "test finished!" << endl;return EXIT_SUCCESS;
}

参考

官方文档: http://docs.mongodb.org/master/MongoDB-Manual-master.pdf

MongoDB 2.2 的 Time To Live (TTL) 集合: http://www.oschina.net/translate/mongodb-time-to-live-ttl-collections

C++客户端代码的示例

mongo小结和使用示例相关推荐

  1. mysql中拼接用什么_MySQL中常用的拼接语句的小结(代码示例)

    本篇文章给大家带来的内容是关于MySQL中常用的拼接语句的小结(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言:在MySQL中 CONCAT ()函数用于将多个字符串 ...

  2. python 美团api接口对接_python实现比对美团接口返回数据和本地mongo数据是否一致示例...

    本文实例讲述了python实现比对美团接口返回数据和本地mongo数据是否一致.分享给大家供大家参考,具体如下: 应用背景:美团平台商品的上下架状态.库存.售价,和mongo库存储的是否一致. too ...

  3. mongoBooster里使用mongo的foreach方法示例

    对于mongo的foreach方法,确实网上还是有一些介绍的,但是和我想在mongoBooster里使用的情况还是不一样,而且看到的举例也都不是很好,这里列一下给大家使用的例子,有个明显的例子理解起来 ...

  4. Spring Data MongoDB示例

    Spring Data MongoDB示例 欢迎使用Spring Data MongoDB示例.Spring Data MongoDB是将Spring Framework与最广泛使用的NoSQL数据库 ...

  5. Spring Cloud Alibaba 完美融合Dubbo-Nacos示例

    原文请关注微信公众号"阿甘正专",获取更多最新文章推荐哦0.0 引言 目前微服务盛行,很多人会把Spring Cloud与Dubbo进行对比,Spring Cloud与Dubbo的 ...

  6. (转)七牛云phpSDK使用笔记

    开发文档:https://developer.qiniu.com/kodo/sdk/1241/php 一.下载官方SDK : https://github.com/qiniu/php-sdk/rele ...

  7. 初学Python——文件操作第二篇

    前言:为什么需要第二篇文件操作?因为第一篇的知识根本不足以支撑基本的需求.下面来一一分析. 一.Python文件操作的特点 首先来类比一下,作为高级编程语言的始祖,C语言如何对文件进行操作? 字符(串 ...

  8. [目录]Pentaho Kettle解决方案:使用PDI构建开源ETL解决方案

    第一部分:开始 1         ETL入门 1.1   OLTP和数据仓库对比 1.2   ETL是什么 1.2.1          ETL解决方案的演化过程 1.2.2          ET ...

  9. 《0 bug ---- C/C++商用工程之道》目录

    扉页 前言 1.1  为什么要写本书? 1.2  本书包括哪些内容 1.3  商用工程开发和软件编程的区别 1.4   商用程序员的核心思想 1.5  本书适合哪些读者看 1.6  本书中一些名词解释 ...

  10. [转]MongoDB c++驱动安装与使用

    安装 获取源码:git clone https://github.com/mongodb/mongo-cxx-driver.git,解压 安装编译工具scons:yum install -y scon ...

最新文章

  1. 【XStream】XStream 忽略不重要点元素
  2. .Net winform中嵌入Flash
  3. Delphi XE2 之 FireMonkey 入门(13) - 动画(下)
  4. bzoj:3110: [Zjoi2013]K大数查询
  5. java中 若干,Java中的随机数发生器。产生若干的复杂性
  6. 【Vegas改编】Winform最小化,系统托盘出现图标,并气泡显示tip
  7. 从一个真实案例看性能差异问题处理方法论
  8. 深度学习6-自定义层详解
  9. python第三方库:使用Jieba对抓取的数据进行中文分词
  10. 【转载】移植TWRP第三方Recovery并刷入
  11. 【Android 教程系列第 31 篇】通过 adb install 命令安装 apk 时提示 signatures do not match previously installed version
  12. 【Captain America Sentinel of Liberty HD】美国队长:自由哨兵 v1.0.2
  13. buct哥德巴赫猜想
  14. Ubuntu 12.04 常用三种输入法及键盘错乱的调整
  15. python实现画小猪佩奇
  16. 新冠全球确诊超2亿!德尔塔后,新「毒王」拉姆达已蔓延32国
  17. 毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
  18. ubuntu16.04 软件更新问题
  19. LINUX内核第一霸
  20. Windows注册表,如何修改

热门文章

  1. python越学越不懂_为什么那么多自学Python的后来都放弃了,总结以下原因
  2. 使用 SQL Server 代理来计划 SSAS 管理任务
  3. callback回调使用 vue_Vue实现剪切板图片压缩
  4. vuejs对象更新渲染_vue 对对象的属性进行修改时,不能渲染页面 vue.$set()
  5. 构建postfix邮件服务器(五)extmail和extman的安装,实现web使用和管理邮件系统...
  6. TCP 拥塞控制算法
  7. Tech-Ed2004的收获
  8. 前端 docker + gitlab CI 的持续集成(三)
  9. 网络爬虫之Xpath用法汇总
  10. nodeJS之TCP模块net