Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active October 20, 2016 07:01
Show Gist options
  • Save LuoZijun/a83bb3c28ffce8e2e78f7a8b161e524b to your computer and use it in GitHub Desktop.
Save LuoZijun/a83bb3c28ffce8e2e78f7a8b161e524b to your computer and use it in GitHub Desktop.
MongoDB Auto-Incrementing Sequence Test

MongoDB 自增整数字段模拟

Date

10/20 2016

官方资料: Create an Auto-Incrementing Sequence Field

Pymongo方法: update_one

结果

运行结果:

Seq: None

[None, 2, 18, 34, 18, 31, 18, 34, 18, 34, 18, 34, 18, 31, 18, 36, 18, 34, 18, 31, 18, 32, 18, 34, 18, 34, 18, 34, 18, 34, 18, 34, 18, 34, 49, 65, 50, 65, 50, 65, 48, 64, 48, 64, 50, 64, 50, 66, 50, 65, 50, 64, 50, 66, 50, 66, 50, 66, 50, 65, 50, 66, 52, 71, 50, 68, 82, 98, 80, 98, 80, 92, 82, 98, 82, 98, 80, 98, 80, 94, 82, 98, 80, 98, 82, 99, 82, 98, 82, 99, 85, 99, 83, 99, 83, 98, 85, 98, 99, 100]

Seq: 100

[101, 117, 118, 134, 117, 129, 117, 133, 117, 133, 114, 128, 117, 131, 118, 135, 118, 134, 118, 134, 118, 134, 121, 138, 118, 133, 117, 133, 118, 136, 117, 134, 130, 144, 139, 158, 145, 164, 145, 160, 149, 164, 147, 164, 150, 167, 151, 165, 151, 167, 149, 167, 152, 167, 151, 167, 153, 168, 154, 169, 154, 169, 152, 169, 158, 172, 172, 189, 172, 193, 180, 197, 177, 195, 180, 195, 184, 197, 184, 198, 182, 199, 182, 199, 184, 197, 184, 198, 186, 199, 184, 199, 186, 199, 186, 199, 193, 199, 199, 200]

我们可以看出自增出来的序列有很多重复,所以,这个方法并不安全!

#!/usr/bin/env python
# coding: utf8
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
from pymongo import MongoClient
import pymongo.cursor
mongodb = MongoClient()
db = mongodb.test
coll = db.counter
def test_multiprocessing():
pool = ThreadPool(16)
query = {'name': 'counter'}
data = {'$inc': {'seq': 1} }
try:
seq_start = str(coll.find_one(query)["seq"])
except:
seq_start = None
print(u"Seq: " + str(seq_start) )
def gen(*args, **kwargs):
ret = coll.update_one(query, data, upsert=True)
if ret.modified_count == 1 and ret.matched_count == 1:
r = coll.find_one(query)
if r:
return r["seq"]
else:
return None
else:
return None
result = pool.map(gen, range(100) )
pool.close()
pool.join()
print(u"Result: ")
print(result)
test_multiprocessing()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment