mongoengine

How to do “insert if not exist else update” with mongoengine?

谁说我不能喝 提交于 2019-11-27 13:39:57
问题 I'm working with mongoengine in Django, this is my document defination: class Location(mongoengine.Document): user_id = mongoengine.IntField(required=True) point = mongoengine.GeoPointField(required=True) I want to do this: given a user_id and a point : if there is no document that have this user_id , create one with the user_id and point and save it; else update the document with user_id with point . Can I do this in one statement with mongoengine? 回答1: Note that get_or_create is now

Mongoengine creation_time attribute in Document

天大地大妈咪最大 提交于 2019-11-27 13:32:13
问题 I am trying to add a creation_time attribute to my documents. The following would be an example: import datetime class MyModel(mongoengine.Document): creation_date = mongo.DateTimeField() modified_date = mongo.DateTimeField(default=datetime.datetime.now) Django models have built in parameter for their DateTimeField objects like add_now , etc., but MongoEngine does not support this. I am wondering if best way to do this is the following: m,created = MyModel.objects.get_or_create() if created:

Sort using MongoEngine?

匆匆过客 提交于 2019-11-27 13:30:59
问题 How do I sort the query objects in MongoEngine, like I would in a regular mongodb query? http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order 回答1: Mongoengine is inspired by Django's ORM, and like Django, it uses order_by to sort the result set. order_by takes a variable number of string arguments, which are the field names (as defined in your documents) optionally preceded by a " - " (to indicate a descending sort, i.e. highest first). For example: class Person(Document): first_name

MongoEngine User authentication (django)

假装没事ソ 提交于 2019-11-27 11:00:38
问题 I am trying to use MongoEngine in a django project I am writing. I am having difficulty getting (or understanding how) the authentication backend works. The user object as far as I can tell is not stored in the request. I have it working but I am not sure if I am doing it in the right/safe way. If someone could look at my code I would be much appreciated. def login(request): user = authenticate(request.POST['username'],request.POST['password']) if user is not None: request.session['user'] =

Django - Auth with mongoengine DB

让人想犯罪 __ 提交于 2019-11-27 01:29:29
问题 I want to handle authentications in my Django project with my mongoengine db. I tried a few examples about this stuff answered in old questions but it didn't run. I'm using Django 1.6 and mongoengine. Everything is installed, running and I can create and save documents to my Mongoengine DB. I'm following http://mongoengine-odm.readthedocs.org/en/latest/django.html And i get the following error: When i run: >>> from django.contrib.auth.models import User >>> user = User.objects.create_user(

MongoEngine: EmbeddedDocument v/s. ReferenceField

眉间皱痕 提交于 2019-11-26 21:59:23
问题 EmbeddedDocument will allow to store a document inside another document, while RefereneField just stores it's reference. But, they're achieving a similar goal. Do they have specific use cases? PS: There's already a question on SO, but no good answers. 回答1: The answer to this really depends on what intend to do with the data you are storing in mongodb. It is important to remember that a ReferenceField will point to a document in another collection in mongodb, whereas an EmbeddedDocument is

Flask throwing 'working outside of request context' when starting sub thread

不羁岁月 提交于 2019-11-26 15:22:18
问题 I am trying to start a new thread in Python inside of a Flask application. I am doing background work that gets triggered by the request, but I don't need to wait for the work to be done to respond to the request. Is it possible to set the flask request in this sub-threat to the request that came in? Reason being, our ACL on our queries to our DB (mongoengine in front of mongoDB) relies on the request's user (it grabs it from flask's request object) to see if they have access to the objects,