mongoengine

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

为君一笑 提交于 2019-12-04 21:43:26
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 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({"items": [{"id": 1}, {"id": 2}, {"id": 3}]}) > db.foo.find({'items.id': 1}, {'items.$': true}) { "_id" :

python wtf AttributeError: 'ObjectIdField' object has no attribute 'help_text'

柔情痞子 提交于 2019-12-04 19:17:07
Based on this tutorial I am trying to create a form to get a few measurements. It seems that the part to display the data is working but when using the model_form command to generate the input form it breaks with this error: File "/myproject/lib/python3.4/site-packages/flask_mongoengine/wtf/orm.py", line 49, in convert 'description': field.help_text or '', AttributeError: 'ObjectIdField' object has no attribute 'help_text' The error happens on this line of my code: form_cls = model_form(Measurement, exclude=('id', 'created_at', 'comments')) This is my view.py code: from flask import Blueprint,

Convert mongodb return object to dictionary

怎甘沉沦 提交于 2019-12-04 15:15:15
问题 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

Filtering an embedded list in MongoEngine

怎甘沉沦 提交于 2019-12-04 11:30:15
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? MongoDB doesn't support exactly this operation that you're requesting, and therefore neither does Mongoengine. You can perform slicing operations on arrays

MongoEngine _types and _cls fields

落花浮王杯 提交于 2019-12-04 06:18:53
Why does mongoengine add _types and _cls fields to every document of a collection. Both of them are a (key, value) pair and both of them contain the name of the document's model class. The only difference is _types value is a list and I assume it can have multiple model class names if there is involved some inheritance. However the question is: why do I need them to exist in every document within a collection when all the documents will have the same values for both fields? Crazyshezy Mongoengine allows Document Inheritance. When defining a class a meta attribute allow_inheritance is used to

Mongodb sort documents by complex computed value

僤鯓⒐⒋嵵緔 提交于 2019-12-04 04:46:39
问题 items = collection.aggregate([ {"$match": {}}, {"$project": { 'temp_score': { "$add": ["$total_score", 100], }, 'temp_votes': { "$add": ["$total_votes", 20], }, 'weight': { "$divide": ["$temp_score", "$temp_votes"] } } } ]) The total_score and total_votes have stored in the document, I can get temp_score and temp_votes as expected, but can't get weight, any suggestion? 回答1: Your $temp_score and $temp_votes are not existing yet in your $divide . You can do another $project : db.user.aggregate(

Mongoengine… query something not in a ListField?

大城市里の小女人 提交于 2019-12-04 02:44:10
for example.. class Page(Document) tags = ListField(StringField()) In this case, we can find out a value in the tags list like this. Page.objects(tags='coding') if tags are like ['coding', 'x', 'y'], then the document will be matched... but My question is how I can find out the value not in the listfield. my incorrect code would be.. Page.objects(tags!='coding') or Page.objects(tags__not = 'coding') or Page.objects(tags__not__in = 'coding') but.. they don't simply work.. how can I query a document that does not have a given value in a ListField? To find any pages that don't have the tags

Upload files to DEFAULT_FILE_STORAGE instead of GridFs with mongoengine

天涯浪子 提交于 2019-12-04 02:21:58
问题 I want to be able to store files in amazon s3. The FileField in mongoengine seems to be hardcoded with gridfs. What can I do to achieve this? Is there a custom filefield out there, that behaves like the regular django FileField? 回答1: I havent seen an S3 FileField for use with MongoEngine - so currently, you'd have to roll your own implementation - you could use a StringField to store the location or you could create your own proxy_class like ImageField but it is quite tided to GridFs but I'm

Mongoengine - how to get database name?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 01:57:28
问题 I am using mongoengine v0.15.0. How to fetch the name of the database connected to? Of course, I would have supplied the name in the uri string. But, is there a way to query mongo and find it? Thanks, Harsha 回答1: All the information about the DB connection created in the mongoengine can be found by calling get_db() which returns a pymongo.database.Database object. Then you can access the database name in the attribute name . Here is an example. from mongoengine.connection import get_db,

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

♀尐吖头ヾ 提交于 2019-12-03 20:25:48
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 relatively low, say, 2000, would it make much of a difference? So far the only thing about the ObjectId