mongo小结和使用示例

简介:

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)
#endif

using namespace std;
using namespace mongo;

int connect_mongo(DBClientConnection* conn, const string& ip, const string& port)
{ 
    try{
        if( conn->getServerAddress().empty() ){   // not init
            string 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;
}
复制代码

本文转自 zhenjing 博客园博客,原文链接:  http://www.cnblogs.com/zhenjing/archive/2013/04/25/mongodb.html   ,如需转载请自行联系原作者

相关文章
|
5月前
|
存储 NoSQL 关系型数据库
Mongo DB 安装及基础操作
MongoDB是一种开源的、基于文档的NoSQL数据库管理系统。它是由10gen(现为MongoDB Inc.)开发和维护的。MongoDB的设计目标是提供灵活的、可扩展的数据存储解决方案,适用于各种类型的应用程序。
85 2
|
7月前
|
NoSQL 数据可视化 关系型数据库
99 # mongo 的基本安装和配置
99 # mongo 的基本安装和配置
29 0
|
JavaScript NoSQL 关系型数据库
mongo 详解修改操作
在mongo中的数组操作是不同的,会有专门的指令来进行修改.
mongo 详解修改操作
|
SQL NoSQL 关系型数据库
mongo 的安装与基本的认识
mongodb属于nosql中的文档型数据库,每个文档相当于是一个对象,它没有列的概念,也没有表关系
mongo 的安装与基本的认识
|
SQL 存储 JSON
【Mongo DB】万字详解,Mongo DB的简介到实战使用(上)
今天主要为大家提供一条龙服务,从Mongo DB的简介到实战使用,使我们面对技术选型的时候可以得心应手。
|
SQL NoSQL 算法
【Mongo DB】万字详解,Mongo DB的简介到实战使用(下)
今天主要为大家提供一条龙服务,从Mongo DB的简介到实战使用,使我们面对技术选型的时候可以得心应手。
|
NoSQL MongoDB Python
Python 操作 mongodb 的简单示例
Python 操作 mongodb 的简单示例
85 0
|
NoSQL 关系型数据库 Shell
Mongo修改数据类型
引言 本文主要讲解Mongodb的类型转换。包括:string转double, string转int, string转Date。
286 0
|
NoSQL 数据库