pymongo

mongoDB references fetching takes time

柔情痞子 提交于 2019-12-13 06:26:22
问题 I use mongoengine as Object-Document mapper. Here is a brief description of the collections that are causing the problem. Each document in collection A, can have a list of references to documents in Collection B. class A(Document): list_b = ListField(EmbeddedDocumentField(EB)) #other fields are not mentioned. class EB(EmbeddedDocument): b_reference = ReferenceField('B') loc = GeoPointField() class B(Document): name = StringField() #other fields are not mentioned. When i try to access the list

PyMongo- selecting sub-documents from collection by regex

笑着哭i 提交于 2019-12-13 06:17:05
问题 Lets take for example the following collections: { '_id': '0', 'docs': [ {'value': 'abcd', 'key': '1234'}, {'value': 'abef', 'key': '5678'} ] } { '_id': '1', 'docs': [ {'value': 'wxyz', 'key': '1234'}, {'value': 'abgh', 'key': '5678'} ] } I want to be able to select only the sub-documents under the 'docs' list which 'value' contains the string 'ab'. What I'm expecting to get is the following collections: { '_id': '0', 'docs': [ {'value': 'abcd', 'key': '1234'}, {'value': 'abef', 'key': '5678'

WSGI post-process cleanup

放肆的年华 提交于 2019-12-13 05:59:19
问题 I'm running a TurboGears app with mod_wsgi and apache, using pymongo wrapped with Ming for persistence. To failproof and scale my app I was interested in using mongo's support for replica sets through a pymongo ReplicaSetConnection. However, as specified in the above links, one must call close() when done with every pymongo ReplicaSetConnection object, otherwise it will leave a bunch of zombie processes floating around (wtf pymongo). This sort of behavior isn't supported very well in Ming (it

How avoid pooling data in memory. When iterate cursor object in pymongo?

半世苍凉 提交于 2019-12-13 05:45:26
问题 How avoid pooling data in memory. When iterate cursor object in pymongo? Example: def iter(): c=pymongo.Connection() cursor=c.db.media.find().skip(0).limit(50000) for item in cursor: yield item Before it goes in cycle for there is pause about 2 minus. It loads all data in memory before start iterate for some reason. Can i somehow avoid it? If I do it in mongodb shell everything is ok. 回答1: Do you know if this is possible? If c.db.media.find() returns everything instead of an iterator, I'm not

How to query documents in mongodb (pymongo) where all keywords exist in a field?

怎甘沉沦 提交于 2019-12-13 05:25:22
问题 I have a list of keywords: keywords = ['word1', 'word2', 'word3'] For now I query for only 1 keyword like this: collection.find({'documenttextfield': {'$regex': ' '+keyword+' '}}) I'm in no way a guru in regex so i do the reggae with spaces on the side of the keyword to find exact match. But what i want now is, having that keywords list, to query the documents and find those which have each of the keywords from the list in the documenttextfield . I have some ideas of how to do this, but they

django-nonrel exclude listfield from admin

六月ゝ 毕业季﹏ 提交于 2019-12-13 04:23:12
问题 I've ran into a typical problem where I have a ListField in a model. I'd like to use the Django admin to play around with the object and the ListField isn't that crucial, it's a list of embedded objects that I can live without. When I use this, I get the error on the main admin page. If I don't use the ModelAdmin object when registering the original Item object, I only get the error if I try to add an Item. from django.contrib import admin class ItemAdmin(admin.ModelAdmin): exclude = ('bids',

how to switch database name in mongoengine

二次信任 提交于 2019-12-13 03:01:46
问题 I have multiple databases with same collection names, document types, etc. In source code at Github, documents use get_db method to choose the database to work on and in that method there is another call to get server connection. Both of these processes works with same parameter that called "alias". Let's say that I have all my db connections defined with same server details and different databases, aliases. In that way, there will be multiple connections to same server. But it would be

How to projection/filter embedded keys with applying regex on key

*爱你&永不变心* 提交于 2019-12-13 02:20:15
问题 I wonder know how to return the specified embedded keys ? by the similar query db.collection.find({"table_name": "change_in_real_gdp", {"data.*.actual":1, "data.*. upper_end_of_range"}}) Original document { "_id": "2012-06-20_change_in_real_gdp", "table_name": "change_in_real_gdp", "data": { "2007": { "actual": "4.8", "upper_end_of_range": "-", "upper_end_of_central_tendency": "-", "lower_end_of_central_tendency": "-", "lower_end_of_range": "-" }, "2008": { "actual": "6.9", "upper_end_of

pymongo==3.0.3: ImportError: No module named connection

断了今生、忘了曾经 提交于 2019-12-13 01:46:44
问题 I just upgraded to pymongo==3.0.3 via pip install --upgrade pymongo , and I'm flooded with ImportError : In [2]: pymongo.version Out[2]: '3.0.3' In [3]: from pymongo import Connection --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-3-dd44bc3249d3> in <module>() ----> 1 from pymongo import Connection ImportError: cannot import name Connection In [4]: from pymongo import connection -------------------------

Server side generated id

心不动则不痛 提交于 2019-12-13 01:42:57
问题 The docs say: Usually, the id will be generated automatically by the database server when the object is save, meaning that you may only access the id field once a document has been saved: >>> page = Page(title="Test Page") >>> page.id >>> page.save() >>> page.id ObjectId('123456789abcdef000000000') But I have found that it's not true: ipdb> > .../lib/python2.7/site-packages/pymongo/collection.py(386)gen() 385 if '_id' not in doc: --> 386 doc['_id'] = ObjectId() 387 So pymongo generates id on