mongoengine

Updating a embedded documents in mongoengine

被刻印的时光 ゝ 提交于 2019-12-03 20:19:11
I have a class mongoengine class Post(EmbeddedDocument): uid = StringField(required=True) text = StringField(required=True) value = StringField() class Feed(Document): label = StringField(required=True) feed_url = StringField(required=True) posts = ListField(EmbeddedDocumentField(Post)) I am trying update a element in Feed(posts) The first: I get a object Feed model = Feed.objects(_id="....").first() Continue I want update Post in model have text = "title". How can I do it with mongoengine? Thanks I resolved it :) Feed.objects(_id="...", posts__text="findvalue").update(set__posts__S__value=

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

和自甴很熟 提交于 2019-12-03 16:08:27
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 mongoengine is based on. My question is: What is the best strategy to avoid this issue with mongoengine?

How can I serialize a MongoDB ObjectId with Marshmallow?

狂风中的少年 提交于 2019-12-03 15:08:00
I'm building and API on top of Flask using marshmallow and mongoengine. When I make a call and an ID is supposed to be serialized I receive the following error: TypeError: ObjectId('54c117322053049ba3ef31f3') is not JSON serializable I saw some ways with other libraries to override the way the ObjectId is treated. I haven't figured it out with Marshmallow yet, does anyone know how to do that? My model is: class Process(db.Document): name = db.StringField(max_length=255, required=True, unique=True) created_at = db.DateTimeField(default=datetime.datetime.now, required=True) My serializer: class

What is the proper way to update a listfield of embedded documents in mongoengine?

随声附和 提交于 2019-12-03 13:28:12
问题 I am trying to define methods for performing checks and updates to a listfield of embedded documents in mongoengine. What is the proper way of doing what I'm trying to do. The code is below. class Comment(EmbeddedDocument): created = DateTimeField() text = StringField() class Post(Document): comments = ListField(EmbeddedDocumentField(Comment)) def check_comment(self, comment): for existing_comment in self.comments: if comment.created == existing_comment.created and comment.text == existing

How to search document by oid in mongoengine

眉间皱痕 提交于 2019-12-03 13:17:24
I need get documents from db by oid, like: Docs.objects(_id='4f4381f4e779897a2c000009') But how to do it, if _id requires ObjectId object and even I try to set ObjectId from pymongo it doesn't work. Docs.objects(_id=pymongo.objectid.ObjectId('4f4381f4e779897a2c000009')) return empty list How about just using the raw string: Docs.objects.get(id='4f4381f4e779897a2c000009') That is probably the easiest way ... right ? This should work: Docs.objects(pk='4f4381f4e779897a2c000009') Came to this question because I had a lot of trouble with this myself. It seems like PyMongo changed this and objectid

mongoengine - Ignore extra fields for schema validation

有些话、适合烂在心里 提交于 2019-12-03 10:40:27
问题 I am trying to query my database. Some records currently have extra fields that are not included in my model schema (by error, but I want to handle these cases). When I try to query the DB and transform the records into the schema, I get the following error: FieldDoesNotExist The field 'X' does not exist on the document 'Y' Because of the extra fields in the database that differ from the schema. Is there a way to ignore this schema validation for extra fields in mongoengine? 回答1: For ignoring

Convert mongodb return object to dictionary

爱⌒轻易说出口 提交于 2019-12-03 09:31:05
I'm using the bottle framework together with mongoengine. I have an orders model : class OrderDetail(Option): orderDetailsQty = FloatField() def to_dict(self): return mongo_to_dict_helper(self) class Order(Document): userName = StringField(required=True) orderDate = DateTimeField() orderStatus = ListField(EmbeddedDocumentField(Status)) orderDetails = ListField(EmbeddedDocumentField(OrderDetail)) orderComments = ListField(EmbeddedDocumentField(Comment)) isActive = BooleanField() def to_dict(self): orderObj = mongo_to_dict_helper(self) orderDetailList = [] for orderDetail in orderObj[

Using MongoEngine Document class methods for custom validation and pre-save hooks

自作多情 提交于 2019-12-03 07:20:28
问题 I am currently exploring the possibilities of the MongoEngine "object document mapper". What is currently not clear to me is to what extent I can move my validation and object creation logic to the Document objects themselves. I have the impression that it should not be a problem, but I'm not finding a lot of examples/caveats/best practices regarding issues as Custom validation functions that are automatically called on save() to evaluate if field contents are valid; Automatic generation of

Updating a list of embedded documents in mongoengine

▼魔方 西西 提交于 2019-12-03 06:53:15
问题 I'm struggling with mongoengine syntax. I have the following models... class Post(EmbeddedDocument): uid = StringField(required=True) text = StringField(required=True) when = DateTimeField(required=True) class Feed(Document): label = StringField(required=True) feed_url = StringField(required=True) posts = ListField(EmbeddedDocumentField(Post)) def my_method(self, post): pass ... and with the post object passed into to my_method, I'd like to update an existing post if it exists in self.posts

mongoengine connect() in settings.py testing problem

雨燕双飞 提交于 2019-12-03 03:56:44
I want to be able to do conditional connect() based on either I started django in testing mode or not. in my settings.py I use mongoengine connect() method to connect to my database but the problem is that I don't want to do that if I ran manage.py test Is there any way I can check if settings.py is being imported from tests or not, some flag maybe. something like if not IN_TESTS: connect() I'm solving this with a custom test runner. Here is an example I based my solution off of: https://github.com/xintron/django-mongorunner/blob/master/mongorunner/testrunner.py This has the advantage of