不断变化的mongoDB结果集

来源:互联网 发布:深圳古月网络 编辑:程序博客网 时间:2024/05/16 10:50

在前几天的一次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
原创粉丝点击