不断变化的mongoDB结果集

在前几天的一次php+mongoDB数据库实做中,遇到了一个很奇怪的问题:

有N张collection,每个collection中有Mn条记录,我先循环N,去每张collection中find()到所有记录,然后在针对每条记录做update()操作,

$mo = new Mongo();

$db =  $mo->dbname;

for($i=0;$i<100;$i++){

$coll = $db->selectCollection(’col’.$i){

$cursor = $coll->find();

while($cursor as $k=>$v){

$uid = $v['uid'];

$rs = $coll->update(array(’uid’=>$uid),array(要更新的内容));

}

}

}

但是实际上,有些记录被update的两次,百思不得其解,update()的$option换了所有方式,都无效。

后来,在Sam的帮助下,详细查了php手册的mongoDB一段,最后发现:

http://www.php.net/manual/en/mongocursor.snapshot.php

MongoCursorMongoCursor::snapshot()

Use snapshot mode for the query. Snapshot mode assures no duplicates are returned, or objects missed, which were present at both the start and end of the query’s execution (if an object is new during the query, or deleted during the query, it may or may not be returned, even with snapshot mode).
Note that short query responses (less than 1MB) are always effectively snapshotted.
Currently, snapshot mode may not be used with sorting or explicit hints.

大意是:$cursor->snapshot();之后,再插入或者删除符合条件的记录时,获取的结果集将不再变化。如果是小于1M的结果集会自动被当作snapshot来处理。

结论:

1,如果你想或的固定的结果集,那么在find()之后要snapshot()一下,保证一致性。

2,不管是否小于1M,这一点在我们开发的时候可能不好评估,所以,只要是要固定结果集的,都snapshot好了。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值