就项目中使用mongodb最常用的接口封装了一下,在初始化连接时可设置支持多线程,使用的是mongodb内部连接池,注意未做异常处理:
1 // mongodb_test.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "DBCore.h" 6 #include <boost/timer.hpp> 7 8 using namespace mongo; 9 10 int _tmain(int argc, _TCHAR* argv[]) 11 { 12 DBCore db; 13 std::string dbname("testdb"); 14 db.init(std::string("127.0.0.1:27017"), dbname); 15 db.ensure_index(dbname, BSON("_id"<<1), "_index_id"); 16 17 int tcont = 1000; 18 printf("test count: %d\n", tcont); 19 boost::timer tt; 20 21 for (int i = 0; i<tcont;i++) 22 { 23 db.insert(dbname, BSON("_id"<< i<< "_str"<<"test")); 24 } 25 26 printf("insert time: %lf(s)\n", tt.elapsed()); 27 28 tt.restart(); 29 for (int i = 0; i<tcont;i++) 30 { 31 db.update(dbname, BSON("_id"<<i), BSON("_str"<<"abcd")); 32 } 33 34 printf("update time: %lf(s)\n", tt.elapsed()); 35 36 37 tt.restart(); 38 for (int i = 0; i<tcont;i++) 39 { 40 db.find(dbname, BSON("_id"<< i)); 41 } 42 43 printf("find time: %lf(s)\n", tt.elapsed()); 44 45 tt.restart(); 46 for (int i = 0; i<tcont;i++) 47 { 48 db.remove(dbname, BSON("_id"<< i)); 49 } 50 51 printf("remove time: %lf(s)\n", tt.elapsed()); 52 53 getchar(); 54 55 return 0; 56 }
1 #pragma once 2 3 #include <mongo/db/jsobj.h> 4 5 #include <boost/smart_ptr.hpp> 6 #include <string> 7 #include <vector> 8 9 #ifdef _DEBUG 10 #pragma comment(lib, "mongodb_driver_d.lib") 11 #else 12 #pragma comment(lib, "mongodb_driver.lib") 13 #endif // DEBUG 14 15 #pragma comment(lib, "dbghelp.lib") 16 17 namespace mongo 18 { 19 class ScopedDbConnection; 20 } 21 22 class DBCore 23 { 24 public: 25 DBCore(void); 26 ~DBCore(void); 27 28 void init(std::string& _db_addr, std::string& _db_name, bool _thread = false); 29 30 std::string insert(const std::string& table_name, const mongo::BSONObj& bObj); 31 32 mongo::BSONObj find(const std::string& table_name, const mongo::BSONObj& bObj, const mongo::BSONObj* bField = nullptr); 33 34 void find(std::vector<mongo::BSONObj>& vec,const std::string& table_name, const mongo::BSONObj& bObj, const mongo::BSONObj* bField = nullptr, 35 int toReturn = 0, int toSkip = 0, const mongo::BSONObj* bSort = nullptr); 36 37 std::string update(const std::string& table_name, const mongo::BSONObj& bObj, const mongo::BSONObj& bUp, bool upsert = true, bool multi = false); 38 39 std::string remove(const std::string& table_name, const mongo::BSONObj& bObj, bool justone = false); 40 41 bool ensure_index(const std::string& table_name, const mongo::BSONObj& bKey, const std::string& index_name, bool unique = false); 42 43 private: 44 boost::shared_ptr<mongo::ScopedDbConnection> get_conn(bool _thread = false); 45 46 std::string db_name; 47 std::string db_addr; 48 std::string db_err; 49 bool b_init; 50 bool b_thread; 51 boost::shared_ptr<mongo::ScopedDbConnection> m_conn; 52 };
1 #include "stdafx.h" 2 #include "DBCore.h" 3 4 #include <mongo/client/connpool.h> 5 #include <boost/bind.hpp> 6 7 using namespace mongo; 8 using namespace std; 9 10 DBCore::DBCore(void) 11 { 12 b_init = false; 13 db_err = "db not init!"; 14 b_thread = false; 15 m_conn = nullptr; 16 } 17 18 19 DBCore::~DBCore(void) 20 { 21 } 22 23 24 void release_conn(ScopedDbConnection* dbconn) 25 { 26 if(dbconn) 27 { 28 dbconn->done(); 29 delete dbconn; 30 dbconn = nullptr; 31 } 32 } 33 34 void DBCore::init(string& _db_addr, string& _db_name, bool _thread) 35 { 36 db_addr = _db_addr; 37 db_name = _db_name + "."; 38 db_err = "db null conn!"; 39 b_thread = _thread; 40 b_init = true; 41 42 if(!b_thread) 43 m_conn = get_conn(true); 44 } 45 46 string DBCore::insert(const string& table_name, const BSONObj& bObj) 47 { 48 auto pcon = get_conn(); 49 if(pcon && pcon->get()) 50 { 51 pcon->get()->insert(db_name+table_name, bObj); 52 return pcon->get()->getLastError(); 53 } 54 return db_err; 55 } 56 57 BSONObj DBCore::find(const string& table_name, const BSONObj& bObj, const BSONObj* bField) 58 { 59 auto pcon = get_conn(); 60 BSONObj b; 61 if(pcon && pcon->get()) 62 { 63 auto ret = pcon->get()->query(db_name+table_name, Query(bObj), 1, 0, bField); 64 if(ret->more()) 65 { 66 b = ret->nextSafe().copy(); 67 } 68 ret.reset(); 69 } 70 return b; 71 } 72 73 void DBCore::find(vector<BSONObj>& vec,const string& table_name, const BSONObj& bObj, const BSONObj* bField, 74 int toReturn, int toSkip, const BSONObj* bSort) 75 { 76 auto pcon = get_conn(); 77 if(pcon && pcon->get()) 78 { 79 Query q(bObj); 80 if(bSort) 81 { 82 q = q.sort(*bSort); 83 } 84 vec.reserve(toReturn); 85 auto ret = pcon->get()->query(db_name+table_name, q, toReturn, toSkip, bField); 86 while(ret->more()) 87 { 88 vec.push_back(ret->nextSafe().copy()); 89 } 90 ret.reset(); 91 } 92 } 93 94 string DBCore::update(const string& table_name, const BSONObj& bObj, const BSONObj& bUp, bool upsert, bool multi) 95 { 96 auto pcon = get_conn(); 97 if(pcon && pcon->get()) 98 { 99 pcon->get()->update(db_name+table_name, Query(bObj), bUp, upsert, multi); 100 return pcon->get()->getLastError(); 101 } 102 return db_err; 103 } 104 105 string DBCore::remove(const string& table_name, const BSONObj& bObj, bool justone) 106 { 107 auto pcon = get_conn(); 108 if(pcon && pcon->get()) 109 { 110 pcon->get()->remove(db_name+table_name, Query(bObj), justone); 111 return pcon->get()->getLastError(); 112 } 113 return db_err; 114 } 115 116 bool DBCore::ensure_index(const string& table_name, const BSONObj& bKey, const string& index_name, bool unique) 117 { 118 auto pcon = get_conn(); 119 if(pcon && pcon->get()) 120 { 121 return pcon->get()->ensureIndex(db_name+table_name, bKey, unique, index_name); 122 } 123 return false; 124 } 125 126 boost::shared_ptr<ScopedDbConnection> DBCore::get_conn(bool _thread) 127 { 128 if(!b_init) 129 return nullptr; 130 131 if(b_thread || _thread) 132 return boost::shared_ptr<ScopedDbConnection>(ScopedDbConnection::getScopedDbConnection(db_addr), boost::bind(&release_conn, _1)); 133 134 return m_conn; 135 }