代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
MongoDB的简单使用
作者:
baoer1024
/ 发布于
2012/3/6
/
715
连接MongoDB,查询是否有给定的值,如果没有,插入到数据库中。
ifndef _MONGO_H_</div> <div>define _MONGP_H_</div> #include <mongo/client/dbclient.h></div> <div>using namespace std;</div> <div>using namespace mongo;</div> <div></div> <div>class Cmongodb</div> <div>{</div> <div>public:</div> <div> Cmongodb(void);</div> <div>public:</div> <div> void Init(string strip, int port, string tbname);</div> <div> bool ConnectServer(string & strerrmsg);</div> <div> bool IsFailedState();</div> <div> bool QueryTarget(string strhash,string & strerrmsg);</div> <div> bool UpdateTime(string strhash,string & strerrmsg);</div> <div> bool QueryandSet(string strhash,string & strerrmsg);</div> <div> bool QueryandInsertSet(string strhash,string & strerrmsg);</div> <div></div> <div> bool InsertTarget(string strhash,string & strerrmsg);</div> <div> void set_action_interval(int);</div> <div>private:</div> <div> string m_strip;</div> <div> int m_port;</div> <div> string m_tbname;</div> <div> int m_action_interval;</div> <div> DBClientConnection m_conn;</div> <div>};</div> <div></div> <div></div> <div>Cmongodb::Cmongodb(void){</div> <div> m_action_interval = 10;</div> <div>} <div></div> <div>void Cmongodb::set_action_interval(int c){</div> <div> printf("%d\n",c);</div> <div> m_action_interval = c;</div> <div>} <div></div> <div></div> <div>void Cmongodb::Init(string strip, int port, string tbname){</div> <div> m_strip = strip;</div> <div> m_port = port;</div> <div> m_tbname = tbname;</div> <div>} <div></div> <div>bool Cmongodb::ConnectServer(string & strerrmsg){</div> <div></div> <div> char szDest[128]={0};</div> <div> sprintf(szDest,"%s:%d",m_strip.c_str(),m_port);</div> <div> if (!m_conn.connect(szDest, strerrmsg)) {</div> <div> return false;</div> <div> } <div> return true;</div> <div>} <div></div> <div>bool Cmongodb::IsFailedState(){</div> <div> string str = m_conn.getServerAddress();</div> <div> if(m_conn.getServerAddress().empty())</div> <div> return true;</div> <div> return m_conn.isFailed();</div> <div>} <div></div> <div></div> <div></div> <div>bool Cmongodb::QueryTarget(string strhash ,string & strerrmsg){</div> <div></div> <div> mongo::BSONObjBuilder query;</div> <div> try{</div> <div> auto_ptr<mongo::DBClientCursor> cursor = m_conn.query(m_tbname,QUERY("_id" << strhash));</div> <div> return cursor->more();</div> <div> } <div> catch(DBException &e ){</div> <div> strerrmsg =e.what();</div> <div> return true;</div> <div> } <div> return true;</div> <div>} <div></div> <div></div> <div>bool Cmongodb::QueryandInsertSet(string strhash ,string & strerrmsg){</div> <div> mongo::BSONObjBuilder query;</div> <div> try{</div> <div> auto_ptr<mongo::DBClientCursor> cursor = m_conn.query(m_tbname,QUERY("_id" << strhash));</div> <div> time_t now_t = time(NULL);</div> <div> long long now_ll = now_t;</div> <div> if(cursor->more()){</div> <div> BSONObj p = cursor->next();</div> <div> int count = p.getIntField("count");</div> <div> count ++;</div> <div> m_conn.update(m_tbname,BSON("_id" << strhash), BSON("count" << count << "time"<< now_ll));</div> <div> if(count %10==0)</div> <div> return false;</div> <div> else</div> <div> return true;</div> <div> }else{</div> <div> m_conn.update(m_tbname,BSON("_id" << strhash), BSON("count" << 1 << "time"<< now_ll));</div> <div> } <div> return false;</div> <div> } <div> catch(DBException &e ){</div> <div> strerrmsg =e.what();</div> <div> return true;</div> <div> } <div>} <div></div> <div></div> <div>bool Cmongodb::InsertTarget(string strhash,string & strerrmsg){</div> <div></div> <div></div> <div></div> <div> BSONObjBuilder query;</div> <div> try{</div> <div> query << "_id" << strhash;</div> <div> m_conn.insert(m_tbname, query.obj());</div> <div> } <div> catch(DBException &e ){</div> <div> strerrmsg = e.what();</div> <div> return false;</div> <div> } <div> return true;</div> <div>} <div>endif</div> <div></div>
试试其它关键字
MongoDB
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
baoer1024
贡献的其它代码
(
5
)
.
用alarm检测并处理代码执行超时
.
变态组合数C(n,m)求解
.
计算字符串的MD5值
.
MongoDB的简单使用
.
KMP求匹配个数
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3