mongoengine

mongoengine query a list of embedded documents

允我心安 提交于 2019-12-22 11:17:11
问题 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

Flask MongoEngine - How to change database?

白昼怎懂夜的黑 提交于 2019-12-22 09:58:01
问题 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 =

python - sort mongodb by the value of one key

偶尔善良 提交于 2019-12-22 09:02: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

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

烂漫一生 提交于 2019-12-22 05:23:28
问题 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".

MongoEngine — how to custom User model / custom backend for authenticate()

非 Y 不嫁゛ 提交于 2019-12-22 02:03:59
问题 SUMMARY How do I use a custom User model and a custom authentication backend (to allow for email / password authentication) with Django + MongoEngine? (Is a custom backend even necessary for that? ...i.e., to use an email for username when authenticating with MongoEngine.) Is there any documentation with a straight-forward (and complete!) example of using a custom user object while using Mongo as the primary datastore when authenticating in Django? (Postgres has such clearer and more

Getting a dictionary inside a list by key in mongoDB (mongoengine)

蹲街弑〆低调 提交于 2019-12-22 00:03:18
问题 I'm using mongoDB (mongoHQ) in my Flask app (mongoengine). I have a Document that looks like this: {items: [{id: 1}, {id: 2}, {id: 3}]} Is there a way to to reach, for example, the dict with id: 1 in a single query ? Currently I'm looping through the items list with a next() statement and I was hoping for a wiser solution. Thanks 回答1: I'm not familiar with MongoEngine, but the $ projection operator can filter an array to show only the matched element. In the Mongo shell: > db.foo.insert({

Mongo Embedded Document Query

隐身守侯 提交于 2019-12-21 14:33:04
问题 I've 2 DynamicDocuments: class Tasks(db.DynamicDocument): task_id = db.UUIDField(primary_key=True,default=uuid.uuid4) name = db.StringField() flag = db.IntField() class UserTasks(db.DynamicDocument): user_id = db.ReferenceField('User') tasks = db.ListField(db.ReferenceField('Tasks'),default=list) I want to filter the UserTasks document by checking whether the flag value (from Tasks Document) of the given task_id is 0 or 1 , given the task_id and user_id. So I query in the following way:- obj

Mongo Embedded Document Query

。_饼干妹妹 提交于 2019-12-21 14:32:43
问题 I've 2 DynamicDocuments: class Tasks(db.DynamicDocument): task_id = db.UUIDField(primary_key=True,default=uuid.uuid4) name = db.StringField() flag = db.IntField() class UserTasks(db.DynamicDocument): user_id = db.ReferenceField('User') tasks = db.ListField(db.ReferenceField('Tasks'),default=list) I want to filter the UserTasks document by checking whether the flag value (from Tasks Document) of the given task_id is 0 or 1 , given the task_id and user_id. So I query in the following way:- obj

Updating a embedded documents in mongoengine

我是研究僧i 提交于 2019-12-21 05:52:05
问题 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?

MongoEngine specify read preference on query

青春壹個敷衍的年華 提交于 2019-12-20 03:11:00
问题 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