Skip to content

Instantly share code, notes, and snippets.

Created June 17, 2016 12:13
python asyncio motor
import asyncio
import motor.motor_asyncio
async def update_sorted_friends(user, cache, collect):
assert isinstance(cache, list), "uids isn't list" # list of dicts
result = await collect.update({"user":{"$in": [i["user"] for i in cache]}}, cache)
print('set_sorted_friends: %s' % repr(result))
return result
hosts = "127.0.0.1:27017"
client = motor.motor_asyncio.AsyncIOMotorClient(hosts, connectTimeoutMS=2000)
db = client.prison
chains = db.sorted_friends
doc = [{"user": "1", "cache": {"10": 10, "11": 11, "12": 12}},
{"user": "2", "cache": {"13": 13, "14": 14, "15": 15}}]
loop = asyncio.get_event_loop()
loop.run_until_complete(update_sorted_friends("777", doc, chains))
loop.close()
@yarkovaleksei
Copy link

Заменить:
result = await collect.update({"user":{"$in": [i["user"] for i in cache]}}, cache)
На:
result = await collect.update({"user":{"$in": [i["user"] for i in cache]}}, cache, multi=True, upsert=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment