通过rsync+inotify实现数据的实时备份(多台备份机)

在前面的博文中,我讲到过利用rsync实现数据的镜像和备份,但是要实现数据的实时备份,单独靠rsync还不能实现,本文就讲述下如何实现数据的实时备份。

一、rsync的优点与不足

与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

Read More

记一次MongoDB性能问题

下面文章转载自火丁笔记,原作者描述了一次MongoDB数据迁移过程中遇到的性能问题及其解决方案,中间追查问题的方法和工具值得我们学习。下面是其原文:

最近忙着把一个项目从MySQL迁移到MongoDB,在导入旧数据的过程中,遇到了些许波折,犯了不少错误,但同时也学到了不少知识,遂记录下来。

公司为这个项目专门配备了几台高性能务器,清一色的双路四核超线程CPU,外加32G内存,运维人员安装好MongoDB后,就交我手里了,我习惯于在使用新服务器前先看看相关日志,了解一下基本情况,当我浏览MongoDB日志时,发现一些警告信息:

WARNING: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl –interleave=all mongod [other options]

当时我并不太清楚NUMA是什么东西,所以没有处理,只是把问题反馈给了运维人员,后来知道运维人员也没有理会这茬儿,所以问题的序幕就这样拉开了。

Read More

mongodb慢查询记录

在 MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是Mongo Database Profiler.不仅有,而且还有一些比MySQL的Slow Query Log更详细的信息。它就是我们这篇文章的主题。

开启 Profiling 功能

有两种方式可以控制 Profiling 的开关和级别,第一种是直接在启动参数里直接进行设置。

启动MongoDB时加上 –profile=级别 即可。

也可以在客户端调用db.setProfilingLevel(级别) 命令来实时配置。可以通过db.getProfilingLevel()命令来获取当前的Profile级别。

Read More

PHP操作MongoDB学习笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* PHP操作MongoDB学习笔记
*/
//*************************
//** 连接MongoDB数据库 **//
//*************************
//格式=>("mongodb://用户名:密码 @地址:端口/默认指定数据库",参数)
$conn = new Mongo();
//可以简写为
//$conn=new Mongo(); #连接本地主机,默认端口.
//$conn=new Mongo("172.21.15.69"); #连接远程主机
//$conn=new Mongo("xiaocai.loc:10086"); #连接指定端口远程主机
//$conn=new Mongo("xiaocai.loc",array("replicaSet"=>true)); #负载均衡
//$conn=new Mongo("xiaocai.loc",array("persist"=>"t")); #持久连接
//$conn=new Mongo("mongodb://sa:123@localhost"); #带用户名密码
//$conn=new Mongo("mongodb://localhost:27017,localhost:27018"); #连接多个服务器
//$conn=new Mongo("mongodb:///tmp/mongo-27017.sock"); #域套接字
//$conn=new Mongo("mongodb://admin_miss:miss@localhost:27017/test",array('persist'=>'p',"replicaSet"=>true)); #完整
//详细资料:http://www.php.net/manual/en/mongo.connecting.php
//*************************
//** 选择数据库与表 **//
//*************************
$db=$conn->mydb; #选择mydb数据库
//$db=$conn->selectDB("mydb"); #第二种写法
$collection=$db->column; #选择集合(选择'表')
//$collection=$db->selectCollection('column'); #第二种写法
//$collection=$conn->mydb->column; #更简洁的写法
//注意:1.数据库和集合不需要事先创建,若它们不存在则会自动创建它们.
// 2.注意错别字,你可能会无意间的创建一个新的数据库(与原先的数据库混乱).


//*************************
//** 插入文档 **//
//*************************
//**向集合中插入数据,返回bool判断是否插入成功. **/
$array=array('column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); #简单插入
echo "新记录ID:".$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:bool(true)
//**向集合中安全插入数据,返回插入状态(数组). **/
$array=array('column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2');
$result=$collection->insert($array,true); #用于等待MongoDB完成操作,以便确定是否成功.(当有大量记录插入时使用该参数会比较有用)
echo "新记录ID:".$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
//**完整的写法 **/
#insert($array,array('safe'=>false,'fsync'=>false,'timeout'=>10000))
/*
* *
* 完整格式:insert ( array $a [, array $options = array() ] )
* insert(array(),array('safe'=>false,'fsync'=>false,'timeout'=>10000))
* 参数:safe:默认false,是否安全写入
* fsync:默认false,是否强制插入到同步到磁盘
* timeout:超时时间(毫秒)
*
* 插入结果:{ "_id" : ObjectId("4d63552ad549a02c01000009"), "column_name" : "col770", "column_exp" : "xiaocai" }
* '_id'为主键字段,在插入是MongoDB自动添加.
*
* 安全插入:
* (注:safe已弃用,建议安全插入使用"w"=>1(默认),非安全操作为"w"=>0,此时虽然不能往数据库插入数据,但是不会输出任何提示
* $person = array("name" => "Joe", "age" => 20);
* $collection->insert($person);
* // 现在 $person 【已经】具有了一个_id字段,所以我们再次保存它的时候,将会得到一个异常
* try {
* $collection->insert($person, array("w" => 1));
* } catch(MongoCursorException $e) {
* echo "Can't save the same person twice!\n";
* }
*
* 详细资料:http://www.php.net/manual/zh/mongocollection.insert.php
* *
*/


//*************************
//** 更新文档 **//
//*************************
//** 修改更新 **/
$where=array('column_name'=>'col123');
$newdata=array('column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array('$set'=>$newdata)); #$set:让某节点等于给定值,类似的还有$pull $pullAll $pop $inc,在后面慢慢说明用法
/*
* 结果:
* 原数据
* {"_id":ObjectId("4d635ba2d549a02801000003"),"column_name":"col123","column_exp":"xiaocai"}
* 被替换成了
* {"_id":ObjectId("4d635ba2d549a02801000003"),"column_name":"col123","column_exp":"GGGGGGG","column_fid":444}
*/
//** 替换更新 **/
$where=array('column_name'=>'col709');
$newdata=array('column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);
/*
* 结果:
* 原数据
* {"_id":ObjectId("4d635ba2d549a02801000003"),"column_name":"col709","column_exp":"xiaocai"}
* 被替换成了
* {"_id":ObjectId("4d635ba2d549a02801000003"),"column_exp":"HHHHHHHHH","column_fid":123}
*/
//** 批量更新 **/
$where=array('column_name'=>'col');
$newdata=array('column_exp'=>'multiple','91u'=>684435);
$result=$collection->update($where,array('$set'=>$newdata),array('multiple'=>true));
/**
* 所有'column_name'='col'都被修改
*/
//** 自动累加 **/
$where=array('91u'=>684435);
$newdata=array('column_exp'=>'edit');
$result=$collection->update($where,array('$set'=>$newdata,'$inc'=>array('91u'=>-5)));
/**
* 更新91u=684435的数据,并且91u自减5
*/
/** 删除节点 **/
$where=array('column_name'=>'col685');
$result=$collection->update($where,array('$unset'=>array('column_exp'=>1)));
/**
* 删除节点column_exp
*/
/*
* *
* 完整格式:update(array $criteria, array $newobj [, array $options = array() ] )
* 注意:1.注意区分替换更新与修改更新
* 2.注意区分数据类型如 array('91u'=>'684435')与array('91u'=>684435)
* 详细资料:http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
*/


//*************************
//** 删除文档 **//
//*************************
/** 清空数据库 **/
$collection->remove(array('column_name'=>'col399'));
//$collection->remove(); #清空集合
/** 删除指定MongoId **/
$id = new MongoId("4d638ea1d549a02801000011");
$collection->remove(array('_id'=>(object)$id));


/*
* *
* 使用下面的方法来匹配{"_id":ObjectId("4d638ea1d549a02801000011")},查询、更新也一样
* $id = new MongoId("4d638ea1d549a02801000011");
* array('_id'=>(object)$id)
* *
*/
//*************************
//** 查询文档 **//
//*************************
/** 查询文档中的记录数 **/
echo 'count:'.$collection->count()."<br>"; #全部
echo 'count:'.$collection->count(array('type'=>'user'))."<br>"; #可以加上条件
echo 'count:'.$collection->count(array('age'=>array('$gt'=>50,'$lte'=>74)))."<br>"; #大于50小于等于74
echo 'count:'.$collection->find()->limit(5)->skip(0)->count(true)."<br>"; #获得实际返回的结果数
/**
* 注:$gt为大于、$gte为大于等于、$lt为小于、$lte为小于等于、$ne为不等于、$exists不存在
*/
/** 集合中所有文档 **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $key => $value) {
echo "$key: "; var_dump($value); echo "<br>";
}
/**
* 注意:
* 在我们做了find()操作,获得$cursor游标之后,这个游标还是动态的.
* 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$cursor 获得.
* 如果你想在获得$cursor之后的结果集不变化,需要这样做:
* $cursor = $collection->find();
* $cursor->snapshot();
* 详见http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html
*/
/** 查询一条数据 **/
$cursor = $collection->findOne();
/**
* 注意:findOne()获得结果集后不能使用snapshot(),fields()等函数;
*/
/** age,type 列不显示 **/
$cursor = $collection->find()->fields(array("age"=>false,"type"=>false));
/** 只显示user 列 **/
$cursor = $collection->find()->fields(array("user"=>true));
/**
* 我这样写会出错:$cursor->fields(array("age"=>true,"type"=>false));
*/
/** (存在type,age节点) and age!=0 and age<50 **/
$where=array('type'=>array('$exists'=>true),'age'=>array('$ne'=>0,'$lt'=>50,'$exists'=>true));
$cursor = $collection->find($where);
/** 分页获取结果集 **/
$cursor = $collection->find()->limit(5)->skip(0);
/** 排序 **/
$cursor = $collection->find()->sort(array('age'=>-1,'type'=>1)); ##1表示降序 -1表示升序,参数的先后影响排序顺序
/** 索引 **/
$collection->ensureIndex(array('age' => 1,'type'=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array('age' => 1,'type'=>-1),array('background'=>true)); #索引的创建放在后台运行(默认是同步运行)
$collection->ensureIndex(array('age' => 1,'type'=>-1),array('unique'=>true)); #该索引是唯一的
/**
* ensureIndex (array(),array('name'=>'索引名称','background'=true,'unique'=true))
* 详见:http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
/** 取得查询结果 **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $key => $value) {
$array[]=$value;
}


//*************************
//** 文档聚类 **//
//*************************
//**distinct — 获取集合里指定键的不同值的列表 **/
//详见:http://php.net/manual/zh/mongocollection.distinct.php
$retval = $collection->distinct("age");
var_dump($retval);//返回一个包含不同值的数组
$retval = $collection->distinct("age",array('name'=>'andy'));//多加了一个where条件,返回name=andy的所有age不同的值所组成的数组
var_dump($retval);

//**group — 类似于SQL中的 GROUP BY 分组 **/
//详见:http://php.net/manual/zh/mongocollection.group.php
$keys = array("sex" => 1);//按性别分组
$initial = array("items" => array());
$reduce = "function (obj, prev) { prev.items.push(obj); }";
$condition = array('condition' => array('name' => array( '$ne' => 'andy')));//where条件,可选
$g = $collection->group($keys, $initial, $reduce, $condition);
print_r($g);

//**aggregate — 主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果 **/
//详见:http://php.net/manual/zh/mongocollection.aggregate.php
//http://blog.csdn.net/moqiang02/article/details/39800993
//http://blog.csdn.net/moqiang02/article/details/39544617
$out = $collection->aggregate(
array(
'$group' => array(
'_id' => '$name', //按名称分组
'count' => array('$sum' => 1),
'total' => array('$sum' => '$num')
)
)
);
var_dump($out);

$conn->close(); #关闭连接


/*
关系型数据库与MongoDB数据存储的区别
MySql数据 结构:
CREATE TABLE IF NOT EXISTS `column`(
`column_id` int(16) NOT NULL auto_increment COMMENT '主键',
`column_name` varchar(32) NOT NULL COMMENT '栏目名称',
PRIMARY KEY (`column_id`)
);
CREATE TABLE IF NOT EXISTS `article`(
`article_id` int(16) NOT NULL auto_increment COMMENT '主键',
`article_caption` varchar(15) NOT NULL COMMENT '标题',
PRIMARY KEY(`article_id`)
);
CREATE TABLE IF NOT EXISTS `article_body`(
`article_id` int(16) NOT NULL COMMENT 'article.article_id',
`body` text COMMENT '正文'
);
MongoDB数据结构:
$data=array(
'column_name' =>'default',
'article' =>array(
'article_caption' => 'xiaocai',
'body' => 'xxxxxxxxxx…'
)
);
$inc
如果记录的该节点存在,让该节点的数值加N;如果该节点不存在,让该节点值等 于N
设结构记录结构为 array('a'=>1,'b'=>'t'),想让a加5,那么:
$coll->update(
array('b'=>'t'),
array('$inc'=>array('a'=>5)),
)
$set
让某节点等于给定值
设结构记录结构为 array('a'=>1,'b'=>'t'),b为加f,那么:
$coll->update(
array('a'=>1),
array('$set'=>array('b'=>'f')),
)
$unset
删除某节点
设记录结构为 array('a'=>1,'b'=>'t'),想删除b节点,那么:
$coll->update(
array('a'=>1),
array('$unset'=>'b'),
)
$push
如果对应节点是个数组,就附加一个新的值上去;不存在,就创建这个数组,并附加一个值在这个数组上;如果 该节点不是数组,返回错误。
设记录结构为array('a'=>array(0=>'haha'),'b'=>1),想附加新数据到节点a,那么:
$coll->update(
array('b'=>1),
array('$push'=>array('a'=>'wow')),
)
这样,该记录就会成为:array('a'=>array(0=>'haha',1=>'wow'),'b'=>1)
$pushAll
与$push类似,只是会一次附加多个数值到某节点
$addToSet
如果该阶段的数组中没有某值,就添加之
设记录结构为array('a'=>array(0=>'haha'),'b'=>1),如果想附加新的数据到该节点a,那么:
$coll->update(
array('b'=>1),
array('$addToSet'=>array('a'=>'wow')),
)
如果在a节点中已经有了wow,那么就不会再添加新的,如果没有,就会为该节点添加新的item——wow。
$pop
设该记录为array('a'=>array(0=>'haha',1=>'wow'),'b'=>1)
删除某数组节点的最后一个元素:
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>1)),
)
删除某数组阶段的第一个元素
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>-1)),
)
$pull
如果该节点是个数组,那么删除其值为value的子项,如果不是数组,会返回一个错误。
设该记录为 array('a'=>array(0=>'haha',1=>'wow'),'b'=>1),想要删除a中value为 haha的子项:
$coll->update(
array('b'=>1),
array('$pull=>array('a'=>'haha')),
)
结果为: array('a'=>array(0=>'wow'),'b'=>1)
$pullAll
与$pull类似,只是可以删除一组符合条件的记录。
*/
?>

PHP Mongodb API参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php  
/*** Mongodb类** examples:
* $mongo = new HMongodb("127.0.0.1:11223");
* $mongo->selectDb("test_db");
* 创建索引
* $mongo->ensureIndex("test_table", array("id"=>1), array('unique'=>true));
* 获取表的记录
* $mongo->count("test_table");
* 插入记录
* $mongo->insert("test_table", array("id"=>2, "title"=>"asdqw"));
* 更新记录
* $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"));
* 更新记录-存在时更新,不存在时添加-相当于set
* $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"),array("upsert"=>1));
* 查找记录
* $mongo->find("c", array("title"=>"asdqw"), array("start"=>2,"limit"=>2,"sort"=>array("id"=>1)))
* 查找一条记录
* $mongo->findOne("$mongo->findOne("ttt", array("id"=>1))", array("id"=>1));
* 删除记录
* $mongo->remove("ttt", array("title"=>"bbb"));
* 仅删除一条记录
* $mongo->remove("ttt", array("title"=>"bbb"), array("justOne"=>1));
* 获取Mongo操作的错误信息
* $mongo->getError();
*/

class HMongodb {

//Mongodb连接
var $mongo;

var $curr_db_name;
var $curr_table_name;
var $error;

/**
* 构造函数
* 支持传入多个mongo_server(1.一个出问题时连接其它的server 2.自动将查询均匀分发到不同server)
*
* 参数:
* $mongo_server:数组或字符串-array("127.0.0.1:1111", "127.0.0.1:2222")-"127.0.0.1:1111"
* $connect:初始化mongo对象时是否连接,默认连接
* $auto_balance:是否自动做负载均衡,默认是
*
* 返回值:
* 成功:mongo object
* 失败:false
*/
function __construct($mongo_server, $connect=true, $auto_balance=true)
{
if (is_array($mongo_server))
{
$mongo_server_num = count($mongo_server);
if ($mongo_server_num > 1 && $auto_balance)
{
$prior_server_num = rand(1, $mongo_server_num);
$rand_keys = array_rand($mongo_server,$mongo_server_num);
$mongo_server_str = $mongo_server[$prior_server_num-1];
foreach ($rand_keys as $key)
{
if ($key != $prior_server_num - 1)
{
$mongo_server_str .= ',' . $mongo_server[$key];
}
}
}
else
{
$mongo_server_str = implode(',', $mongo_server);
} }
else
{
$mongo_server_str = $mongo_server;
}
try {
$this->mongo = new Mongo($mongo_server, array('connect'=>$connect));
}
catch (MongoConnectionException $e)
{
$this->error = $e->getMessage();
return false;
}
}

function getInstance($mongo_server, $flag=array())
{
static $mongodb_arr;
if (emptyempty($flag['tag']))
{
$flag['tag'] = 'default'; }
if (isset($flag['force']) && $flag['force'] == true)
{
$mongo = new HMongodb($mongo_server);
if (emptyempty($mongodb_arr[$flag['tag']]))
{
$mongodb_arr[$flag['tag']] = $mongo;
}
return $mongo;
}
else if (isset($mongodb_arr[$flag['tag']]) && is_resource($mongodb_arr[$flag['tag']]))
{
return $mongodb_arr[$flag['tag']];
}
else
{
$mongo = new HMongodb($mongo_server);
$mongodb_arr[$flag['tag']] = $mongo;
return $mongo; } }

/**
* 连接mongodb server
*
* 参数:无
*
* 返回值:
* 成功:true
* 失败:false
*/
function connect()
{
try {
$this->mongo->connect();
return true;
}
catch (MongoConnectionException $e)
{
$this->error = $e->getMessage();
return false;
}
}

/**
* select db
*
* 参数:$dbname
*
* 返回值:无
*/
function selectDb($dbname)
{
$this->curr_db_name = $dbname;
}

/**
* 创建索引:如索引已存在,则返回。
*
* 参数:
* $table_name:表名
* $index:索引-array("id"=>1)-在id字段建立升序索引
* $index_param:其它条件-是否唯一索引等
*
* 返回值:
* 成功:true
* 失败:false
*/
function ensureIndex($table_name, $index, $index_param=array())
{
$dbname = $this->curr_db_name;
$index_param['safe'] = 1;
try {
$this->mongo->$dbname->$table_name->ensureIndex($index, $index_param);
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
}
}

/**
* 插入记录
*
* 参数:
* $table_name:表名
* $record:记录
*
* 返回值:
* 成功:true
* 失败:false
*/
function insert($table_name, $record)
{
$dbname = $this->curr_db_name;
try {
$this->mongo->$dbname->$table_name->insert($record, array('safe'=>true));
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
}
}

/**
* 查询表的记录数
*
* 参数:
* $table_name:表名
*
* 返回值:表的记录数
*/
function count($table_name)
{
$dbname = $this->curr_db_name;
return $this->mongo->$dbname->$table_name->count();
}

/**
* 更新记录
*
* 参数:
* $table_name:表名
* $condition:更新条件
* $newdata:新的数据记录
* $options:更新选择-upsert/multiple
*
* 返回值:
* 成功:true
* 失败:false
*/
function update($table_name, $condition, $newdata, $options=array())
{
$dbname = $this->curr_db_name;
$options['safe'] = 1;
if (!isset($options['multiple']))
{
$options['multiple'] = 0; }
try {
$this->mongo->$dbname->$table_name->update($condition, $newdata, $options);
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
} }

/**
* 删除记录
*
* 参数:
* $table_name:表名
* $condition:删除条件
* $options:删除选择-justOne
*
* 返回值:
* 成功:true
* 失败:false
*/
function remove($table_name, $condition, $options=array())
{
$dbname = $this->curr_db_name;
$options['safe'] = 1;
try {
$this->mongo->$dbname->$table_name->remove($condition, $options);
return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
} }

/**
* 查找记录
*
* 参数:
* $table_name:表名
* $query_condition:字段查找条件
* $result_condition:查询结果限制条件-limit/sort等
* $fields:获取字段
*
* 返回值:
* 成功:记录集
* 失败:false
*/
function find($table_name, $query_condition, $result_condition=array(), $fields=array())
{
$dbname = $this->curr_db_name;
$cursor = $this->mongo->$dbname->$table_name->find($query_condition, $fields);
if (!emptyempty($result_condition['start']))
{
$cursor->skip($result_condition['start']);
}
if (!emptyempty($result_condition['limit']))
{
$cursor->limit($result_condition['limit']);
}
if (!emptyempty($result_condition['sort']))
{
$cursor->sort($result_condition['sort']);
}
$result = array();
try {
while ($cursor->hasNext())
{
$result[] = $cursor->getNext();
}
}
catch (MongoConnectionException $e)
{
$this->error = $e->getMessage();
return false;
}
catch (MongoCursorTimeoutException $e)
{
$this->error = $e->getMessage();
return false;
}
return $result;
}

/**
* 查找一条记录
*
* 参数:
* $table_name:表名
* $condition:查找条件
* $fields:获取字段
*
* 返回值:
* 成功:一条记录
* 失败:false
*/
function findOne($table_name, $condition, $fields=array())
{
$dbname = $this->curr_db_name;
return $this->mongo->$dbname->$table_name->findOne($condition, $fields);
}

/**
* 获取当前错误信息
*
* 参数:无
*
* 返回值:当前错误信息
*/
function getError()
{
return $this->error;
}
}

?>