mongoengine

OperationFailure: database error when threading in MongoEngine/PyMongo

痞子三分冷 提交于 2019-12-07 06:40:15
问题 I have a function that will read data from a website, process it, and then load it into MongoDB. When I run this without threading it works fine but as soon as I set up celery tasks that just call this one function I frequently get the following error: "OperationFailure: database error: unauthorized db:dbname lock type:-1" It's somewhat odd because if I run the non-celery version on multiple terminals, I do not get this error at all. I suspect it has something to do with there not being an

How does MongoEngine handle Indexes (creation, update, removal)?

怎甘沉沦 提交于 2019-12-07 06:12:00
问题 Best practice question about setting Mongo indexes. Mongoengine, the Python ORM wrapper, allows you to set indexes in the Document meta class. When is this meta class introspected and the index added? Can I build a collection via a mongoengine Document class and then add an index after the fact? If I remove the index from the meta class, is the index automatically removed from the corresponding collection? Thanks, 回答1: You can add an index at any time and ensureIndex will be called behind the

python, mongoengine - do like/regex search

ⅰ亾dé卋堺 提交于 2019-12-07 02:21:42
问题 I know I can do a glob-type search on mongodb: db.person.find({ name: /*.bob.*/ }) or db.person.find({ name: { $regex: '*.bob.*' }}) How do I do this with mongoengine without using a raw query (which is apparently the only way based on my searches)? I've blindly tried several variations like: Person.objects(name='/.*bob.*/') Person.objects(name='/\.*bob\.*/') Person.objects(name='.*bob.*') Person.objects(name='\\.*bob\\.*') etc, to no avail... 回答1: It looks like you can do it this way: import

Mongoengine, retriving only some of a MapField

十年热恋 提交于 2019-12-06 09:27:33
For Example.. In Mongodb.. > db.test.findOne({}, {'mapField.FREE':1}) { "_id" : ObjectId("4fb7b248c450190a2000006a"), "mapField" : { "BOXFLUX" : { "a" : "f", } } } The 'mapField' field is made of MapField of Mongoengine. and 'mapField' field has a log of key and data.. but I just retrieved only 'BOXFLUX'.. this query is not working in MongoEngine.... for example.. BoxfluxDocument.objects( ~~ querying ~~ ).only('mapField.BOXFLUX') AS you can see.. only('mapField.BOXFLUX') or only only('mapField__BOXFLUX') does not work. it retrieves all 'mapField' data, including 'BOXFLUX' one.. How can I

MongoEngine: Replacing get_or_create with upsert/update_one

孤者浪人 提交于 2019-12-06 06:49:07
I understand that get_or_create is now deprecated in favour of using upsert , but how do I make update_one to return the object rather the number of objects modified, and can I just retrieve an object if I don't want to update anything? e.g. Model.objects.get_or_create(first_name='John', last_name='Potter', age=40) # assuming that first_name + last_name + age are enough to uniquiely indentify a person returns a Model object (a new object if it didn't exist, and existing object if it does). What would be the equivalent of this using the new method? Model.objects(first_name='John', last_name=

How to format data for MongoEngine PointField

柔情痞子 提交于 2019-12-06 05:56:33
So I wanted to do some experiments with location data in mongodb, so I wrote some python code to generate some testing data. Unfortunately the documentation at http://docs.mongoengine.org/apireference.html#mongoengine.fields.PointField isn't explicit about how to format the input. class Location(db.Document): coord = db.PointField(required=True) # GeoJSON Trying to store a list containing the lng/lat fails: >>> a = Location(coord=[1,2]) >>> a.save() mongoengine.errors.OperationError: Could not save document (location object expected, location array not in correct format) Passing a geoJSON

Filtering an embedded list in MongoEngine

徘徊边缘 提交于 2019-12-06 05:54:26
问题 If I have these models: class Sub(EmbeddedDocument): name = StringField() class Main(Document): subs = ListField(EmbeddedDocumentField(Sub)) I want to have a query that returns the Mains, with the subs being filtered by name existing Main.objects.filter(subs__name__exists=True) This returns the correct Mains, but the Subs are always the entire list, not a subset. How can I get only the subset? Do I need to rely on list comprehensions? 回答1: MongoDB doesn't support exactly this operation that

mongoengine query a list of embedded documents

非 Y 不嫁゛ 提交于 2019-12-06 05:12:41
I'm running into a classic pitfall, but can't find a good example with mongoengine of what I should be doing. Using the standard blog example I have something like: class Comment(EmbeddedDocument): author = StringField() approved = BooleanField(default=False) class Post(Document): id = StringField(required=True, unique=True) comments = ListField(EmbeddedDocumentField(Comment)) For a given blog post (with id some_id ) I just want to load the list of approved comments. I keep accidentally loading all comments if any of the comments for the post are approved, because I'm matching an element of

can't query over ListField(EmbeddedDocumentField)

扶醉桌前 提交于 2019-12-06 05:12:40
I have the following model class Skill(EmbeddedDocument): name = StringField(required = True) level = IntField(required = True) class Agent(Document): name = StringField(required = True) email = EmailField(required = True, unique = True) skills = ListField(EmbeddedDocumentField(Skill)) I want to search for the Agents that have skills with (name = "computer skills and level >5) I have wrote the following query: Agent.objects.filter(name='ashraf', skills__level__gt=5,skills__name="Computer Skills") If an Agent have skill named "Computer skills" with level = 3 and also have a skill named "English

MongoEngine - Pull a reference from a ListField, by id

与世无争的帅哥 提交于 2019-12-06 04:17:14
问题 I would like to remove some references from a ListField(ReferenceField) , solely based on their value. I store information about images in the following Model: class ImageUrl(Document): src = UrlField() counter = IntField() deleted = BooleanField() We store the id s of the images encountered on a page in an EmbeddedDocument called Webpage : class Webpage(EmbeddedDocument): image_list = ListField(ReferenceField(ImageUrl)) ... Finally, the Website model is embedded into a RawData model: class