mongoengine

Invalid embedded document instance provided to an EmbeddedDocumentField on save

假如想象 提交于 2019-12-06 02:49:03
问题 I have these mongoengine models declared: class SyncDiscrepancy(EmbeddedDocument): upi = StringField(primary_key=True) error_code = IntField(required=True) meta = { 'indexes': ['upi', 'error_code'] } ########## END SYNC class Flight(Document): identifier = StringField(primary_key=True) env = StringField(required=True, max_length=3) peak = IntField(required=True) carrier = StringField(required=True, max_length=3) number = IntField(required=True) boardpoint = StringField(required=True) offpoint

How to remove a item from a list(ListField) by id in MongoEngine?

∥☆過路亽.° 提交于 2019-12-06 00:40:57
问题 structure: {title: 'test', comments: [{id:1, title: ''}, {id: 8, title: ''}]} i need remove the id=8 item, thanks. 回答1: Hi you can pull items from an array: https://github.com/hmarr/mongoengine/blob/master/tests/queryset.py#L1374 See $pull: http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull 回答2: You need to use $pull operator here : http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull db.collection.update({'title':'test'},{$pull : { 'comments' : { 'id' : 8 }}); 回答3: Here

Flask MongoEngine - How to change database?

空扰寡人 提交于 2019-12-05 20:43:51
I have Flask app with a Post model which spans across multiple MongoDB databases (db1, db2, db3,...), so I need to be able to query different dbs. I'm using Flask-MongoEngine extension. My __init__.py contains the following line: db = MongoEngine(app) and in config.py I have: MONGODB_SETTINGS = {'DB': 'db1'} I tried the following without success: Alter the connection parameter in the db object like this: db.connection = mongoengine.connect('db2') It didn't change anything. Executing post = Post.objects.all() still ran on the original db ( db1 ). Create alias in the Post class meta like this:

python - sort mongodb by the value of one key

烈酒焚心 提交于 2019-12-05 18:14:50
I have a collection with below data structure: [{name: "123", category: "A"}, {name: "456", category: "B"}, {name: "789", category: "A"}, {name: "101", category: "C"}] I want to be able to sort them according to the value of category , by specifying which comes first. For example, sorting the query in the order of B->C->A, the result would yield: [{name: "456", category: "B"}, {name: "101", category: "C"}, {name: "123", category: "A"}, {name: "789", category: "A"}] Is there any good way of doing so with the mongo query API? I am using mongoengine The best way to do this is using the .aggregate

AttributeError with Django REST Framework and MongoEngine

一个人想着一个人 提交于 2019-12-05 15:12:58
I am trying to use Django and the Django REST Framework together with MongoEngine but it doesn't seem to work for me. I don't know where things go wrong ... perhaps someone can help me out. Here is the code: models.py from mongoengine import * class Lady(Document): firstname = StringField() lastname = StringField() serializers.py from rest_framework import serializers from mongoengine import * class LadySerializer(serializers.Serializer): firstname = serializers.CharField(max_length=50) lastname = serializers.CharField(max_length=50) def restore_object(self,attrs,instance=None): if instance:

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

女生的网名这么多〃 提交于 2019-12-05 08:25:51
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, You can add an index at any time and ensureIndex will be called behind the scenes so it will be added if it doesn't exist. If you remove an index from the meta - you will have to use

python, mongoengine - do like/regex search

陌路散爱 提交于 2019-12-05 07:22:17
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... It looks like you can do it this way: import re regex = re.compile('.*bob.*') Person.objects(name=regex) 来源: https://stackoverflow.com/questions

MongoEngine query list for objects having properties starting with prefixes specified in a list

醉酒当歌 提交于 2019-12-05 06:17:58
I need to query Mongo database for elements that have a certain property beginning with any prefix in the list. Now I have a piece of code like this: query = mymodel(terms__term__in=query_terms) and this matches objects that have an item on a list "terms" that has StringField "term" explicitly occurring on a list "query_terms". What I want to achieve is having objects that have an item on a list "terms" that has StringField "term" beginning with any prefix that occurs on a list "query_terms". Is it possible to do it in one query and without storing every possible prefix of "term" in database?

Performance disadvantage using slug as primary key/_id in mongo?

纵饮孤独 提交于 2019-12-05 02:56:06
问题 Let's take for example a blog post where a unique slug is generated from the post's title: sample_blog_post. Instead of storing a mongo ObjectId as the _id, say you store the slug in the _id. Besides the obvious case where the slug may change if the title changes, are there major disadvantages in terms of performance by using a string instead of a numerical _id? This could become problematic if, say, the number of posts became very large, say, over a million. But if the number of posts was

MongoEngine and dealing with “UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking”

≯℡__Kan透↙ 提交于 2019-12-05 01:02:10
问题 I am using Celery and MongoEngine as part of my Django App with. I am getting this warning, when a celery @shared_task accesses the mongodb database via mongoengine model classes: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False,or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#using-pymongo-with-multiprocessing It clearly has something to do with multiprocessing and pyMongo that is that