tornado-motor

MongoDb with FastAPI

孤街浪徒 提交于 2020-02-06 08:07:20
问题 I am playing around with FastAPI a bit and wanted to connect it to a MongoDB database. I however am confused which ODM to choose between motor which is async and mongoengine. Also, in the NoSQL example here they have created a new bucket and also the called the code to connect to db every time it is used. However, both motor and mongoengine seem to prefer a global connection. So what would be a good way to connect to mongodb? 回答1: I believe you already got your answers in the issue forums of

Failed Aggregation on Tornado/Motor: yielded unknown object MotorAggregationCursor

时光总嘲笑我的痴心妄想 提交于 2019-12-24 12:47:37
问题 I'm having issue to execute MongoDB aggregation operation on Tornado. This is the code, pipeline = [ {'$match': { '$or': [ {'owner.id': '56dfdaa4082024b9384c0055'}, {'members.top.member.id':'56dfdaa4082024b9384c0055'} ] }}, {'$sort': {'date_s': -1}}, {'$skip': 0}, {'$limit': 20}, {'$project':{ 'created_at': 1, 'name': 1, 'id': '$_id', 'group.group_id': 1, '_id': 0, 'permission': 1, 'owner': 1, 'type': 1, 'members.total': 1, 'desc': 1, 'declared': 1 }} ] cursor = yield db.activities.aggregate

RuntimeError: Task attached to a different loop

送分小仙女□ 提交于 2019-12-13 17:22:05
问题 Hi I'm using AsyncIOMotorClient for asynchronous db calls to mongoDb. Below is my code. xyz.py async def insertMany(self,collection_name,documents_to_insert): try: collection=self.database[collection_name] document_inserted = await collection.insert_many(documents_to_insert) return document_inserted except Exception: raise def insertManyFn(self,collection_name,documents_to_insert): try: loop=asyncio.new_event_loop() asyncio.set_event_loop(loop) loop1=asyncio.get_event_loop() inserted

Does Motorengine mantain an IO stream with mongo db?

萝らか妹 提交于 2019-12-11 12:14:10
问题 I want to use Motorengine for my Tornado application. As given in the docs, this is how I am supposed to make the ODM from motorengine.document import Document from motorengine.fields import StringField, DateTimeField class Article(Document): title = StringField(required=True) description = StringField(required=True) published_date = DateTimeField(auto_now_on_insert=True) new_title = "Better Title %s" % uuid4() def create_article(): Article.objects.create( title="Some Article", description=

How to hide _id from Aggregation?

纵饮孤独 提交于 2019-12-04 15:39:17
问题 I've this query: produits = yield motor.Op(db.users.aggregate, [{"$unwind":"$pup"},{"$match":{"pup.spec.np":nomp}}, {"$group":{"_id":"$pup.spec.id","pup":{"$push":"$pup"}}}]) which gives me this result: print produits {u'ok': 1.0, u'result': [{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}]} so I can do: prod = produits["result"] [{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}] but how can I hide "_id" so that I can only get: [{u'pup': [{u'avt': {u'fto': ..all

How to hide _id from Aggregation?

流过昼夜 提交于 2019-12-03 22:24:48
I've this query: produits = yield motor.Op(db.users.aggregate, [{"$unwind":"$pup"},{"$match":{"pup.spec.np":nomp}}, {"$group":{"_id":"$pup.spec.id","pup":{"$push":"$pup"}}}]) which gives me this result: print produits {u'ok': 1.0, u'result': [{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}]} so I can do: prod = produits["result"] [{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}] but how can I hide "_id" so that I can only get: [{u'pup': [{u'avt': {u'fto': ..all the results}}]}] in a normal query I would simply add something like {"_id":0} but here it doesn't

BadYieldError when using find() Motor [MongoDB + Tornado]

可紊 提交于 2019-12-02 12:48:25
问题 I am new to python tornado framework. I have a small collection of data in MongoDB. I am using a simple get function in my python file. I get a BadYieldError when using the db.collection.find() option. But db.collection.find_one() works fine but it display only one record. import tornado import bson from bson import json_util from bson.json_util import dumps class TypeList(APIHandler): @gen.coroutine def get(self): doc = yield db.vtype.find() self.write(json_util.dumps(doc)) The error is:

what happens to variables in tornado coroutines functions?

本秂侑毒 提交于 2019-12-01 09:21:13
I'm new to the concept of non-blocking IO, and there is something i'm having trouble understanding - about coroutines. consider this code: class UserPostHandler(RequestHandler): @gen.coroutine def get(self): var = 'some variable' data = json.loads(self.request.body) yield motor_db.users.insert({self.request.remote_ip: data})#asynch non blocking db insert call #success self.set_status(201) print var when the get function is called, it creates the string var . what happens to this variable when the function waits for the motor.insert to complete? To my understanding "non blocking" implies that

MongoDB return True if document exists

筅森魡賤 提交于 2019-11-28 21:27:35
I want to return true if a userID already exists and false otherwise from my collection.I have this function but it always returns True . def alreadyExists(newID): if db.mycollection.find({'UserIDS': { "$in": newID}}): return True else: return False How could I get this function to only return true if a user id already exists? Philipp Note: This answer is outdated. More recent versions of MongoDB can use the far more efficient method db.collection.countDocuments . See the answer by Xavier Guihot for a better solution. find doesn't return a boolean value, it returns a cursor . To check if that

MongoDB return True if document exists

血红的双手。 提交于 2019-11-27 13:47:26
问题 I want to return true if a userID already exists and false otherwise from my collection.I have this function but it always returns True . def alreadyExists(newID): if db.mycollection.find({'UserIDS': { "$in": newID}}): return True else: return False How could I get this function to only return true if a user id already exists? 回答1: Note: This answer is outdated. More recent versions of MongoDB can use the far more efficient method db.collection.countDocuments. See the answer by Xavier Guihot