NextVal并发问题
func NextVal(countername string) int32 会有bug,当开始同时有两个请求到达时就会插入两条记录 可以是用_id为countername这样可以避免
update操作应该是原子的
你看看 http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ findAndModify Behavior 里面有讲解 你这个gonet还会继续更新吗?
When findAndModify() includes the upsert: true option and the query field(s) is not uniquely indexed, the method could insert a document multiple times in certain circumstances.
是的,两个条件同时满足的时候才会发生。
- upsert == true
- 没有建立unique索引
这个确实需要特别注意。
这个库会持续更新,如果有更好的实践经验,我会归纳到这里。
出现这种的可能性,是记录不存在的时候,有同时进行的upsert,就可能会创建多条记录。但只要已经存在这条记录的时候,是不应该创建出来的。
不过简单起见, 分为两步走更合适:
- 预先创建记录
- 直接$inc操作。
能不能提供一个测试客户端啊