mongoengine

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

自古美人都是妖i 提交于 2019-12-03 03:30:57
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_comment.text: return True return False def add_or_replace_comment(self, comment): for existing_comment in

Update a MongoEngine document using a python dict?

醉酒当歌 提交于 2019-12-03 02:46:21
Is it possible to update a MongoEngine document using a python dict? For example: class Pets(EmbeddedDocument): name = StringField() class Person(Document): name = StringField() address = StringField() pets = ListField(EmbeddedDocumentField(Pets)) p = Person() p.update_with_dict({ "name": "Hank", "address": "Far away", "pets": [ { "name": "Scooter" } ] }) Ok I just made a function for it. You call it like update_document(document, data_dict) . It will loop through the items of data_dict and get the field instance using the key of the data_dict . It will then call field_value(field, value)

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

旧街凉风 提交于 2019-12-02 20:51:23
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 the identifier on save(), based on the hash of the contents of a field; I think I need to override the

Updating a list of embedded documents in mongoengine

怎甘沉沦 提交于 2019-12-02 19:31:41
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 with a matching uid, or push to self.posts if not. Is there syntax to do that in one call in mongoengine

mongoengine connection and multiple databases

我是研究僧i 提交于 2019-12-02 10:09:12
I have 2 databases I want to query from, but I only get results from one. I'm using mongoengine with python and graphene (it's my first time). I've exhausted my search and I don't understand how I can resolve this issue. Here is my code: import graphene from mongoengine import Document, connect from mongoengine.context_managers import switch_collection from mongoengine.fields import ( StringField, UUIDField, IntField, FloatField, BooleanField, ) from graphene_mongo import MongoengineObjectType from mongoengine.connection import disconnect class UserModel(Document): meta = {"collection": "users

using key as value in Mongoengine

随声附和 提交于 2019-12-02 07:23:49
问题 I use mongoengine for mongodb in django. but.. mongoengine fields (like StringField) makes me build up schema toward the way that I don't want. I mean, it strictly insists that I pre-write key name before I do know what it will be. for example... in case that I do not know what key name will be put into database... > for(var i=0; i<10; i++){ ... o = {}; ... o[i.toString()] = i + 100; ... db.test.save(o) ... } > db.test.find() { "_id" : ObjectId("4ed623aa45c8729573313811"), "0" : 100 } { "_id"

Multiple databases (mongodb[mongoengine] and sql ) with django 1.8

最后都变了- 提交于 2019-12-02 07:11:54
问题 I am using mongoengine with Django and my project needs to connect to one instances of MongoDB while another with sql .How my databse section of setting.py should be like ? DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'admin_db', 'USER': 'root', 'PASSWORD': 'root', 'HOST': 'localhost', }, } from mongoengine import connect connect( db='pom', username='admin', password='root', host='mongodb://admin:root@localhost' ) 回答1: You could add multiple databases for your app

Trouble using MongoDB as backend for Django project (Django 1.7)

痞子三分冷 提交于 2019-12-02 04:36:10
I set up my app using the following tutorial, http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html When I set my backend as detailed in this link in my settings.py, I get the following error: NotImplementedError: subclasses of BaseDatabaseIntrospection may require a get_table_list() method I have installed all the necessary packages (django-nonrel, djangotoolbox, mongodb-engine), but I'm still getting these errors. What might I be doing wrong/am I missing? It could very likely be something trivial-- I'm new to Django and MongoDB. My DATABASES setting is as follows,

using key as value in Mongoengine

半腔热情 提交于 2019-12-02 03:11:24
I use mongoengine for mongodb in django. but.. mongoengine fields (like StringField) makes me build up schema toward the way that I don't want. I mean, it strictly insists that I pre-write key name before I do know what it will be. for example... in case that I do not know what key name will be put into database... > for(var i=0; i<10; i++){ ... o = {}; ... o[i.toString()] = i + 100; ... db.test.save(o) ... } > db.test.find() { "_id" : ObjectId("4ed623aa45c8729573313811"), "0" : 100 } { "_id" : ObjectId("4ed623aa45c8729573313812"), "1" : 101 } { "_id" : ObjectId("4ed623aa45c8729573313813"), "2

MongoEngine specify read preference on query

隐身守侯 提交于 2019-12-02 01:53:25
I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows : from pymongo.read_preferences import ReadPreference <collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED) However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax correct? If yes, how do I verify if the secondary is being queried? Figured out what the issue was. In the