mongoengine

What is the difference between EmbeddedDocumentField and ReferenceField in mongoengine

血红的双手。 提交于 2019-11-30 12:26:04
Internally, what are the differences between these two fields? What kind of schema do these fields map to in mongo? Also, how should documents with relations be added to these fields? For example, if I use from mongoengine import * class User(Document): name = StringField() class Comment(EmbeddedDocument): text = StringField() tag = StringField() class Post(Document): title = StringField() author = ReferenceField(User) comments = ListField(EmbeddedDocumentField(Comment)) and call >>> some_author = User.objects.get(name="ExampleUserName") >>> post = Post.objects.get(author=some_author) >>> post

MongoEngine Document Object made using from_json doesn't save

百般思念 提交于 2019-11-30 12:16:10
问题 I am trying to build a document object using from_json method. object.save() throws no error, but the document is not inserted in mongo. On the other hand if I make the object by assigning values to each of the fields, it works fine. I am unable to find the reason for this. Below is the code for both the cases. from flask import Flask from flask.ext.mongoengine import MongoEngine import json, datetime app = Flask(__name__) app.config["MONGODB_SETTINGS"] = {'DB': 'test','host': 'localhost'}

MongoEngine Document Object made using from_json doesn't save

守給你的承諾、 提交于 2019-11-30 02:24:44
I am trying to build a document object using from_json method. object.save() throws no error, but the document is not inserted in mongo. On the other hand if I make the object by assigning values to each of the fields, it works fine. I am unable to find the reason for this. Below is the code for both the cases. from flask import Flask from flask.ext.mongoengine import MongoEngine import json, datetime app = Flask(__name__) app.config["MONGODB_SETTINGS"] = {'DB': 'test','host': 'localhost'} app.config["SECRET_KEY"] = "mySecretKey" db = MongoEngine(app) class User(db.Document): user_id = db

Mongoengine: How to sort Embedded Document list by Embedded document field

我怕爱的太早我们不能终老 提交于 2019-11-29 22:24:22
问题 So what I'm after is something like: class Comment(EmbeddedDocument): content = StringField() upvotes = IntField() pub_date = DateTimeField() class Post(Document): title = StringField() comments = SortedListField(EmbeddedDocumentField(Comment)) post_date = DateTimeField() By default, this sorts by the chronological order of comment submission, but I want to make the SortedListField sort by the upvotes attribute of the embedded comment documents. Is this possible, and if so how do I go about

MongoDB using an OR clause in mongoengine

ⅰ亾dé卋堺 提交于 2019-11-29 13:26:32
I'm using python's mongoengine to query MongoDB, and have loved it for the most part, but I'm having an issue with an advanced query . Here's my model class ContentItem(Document): account = ReferenceField(Account) creator = ReferenceField(User) public = BooleanField(default=False) last_used = DateTimeField(default=datetime.now) I would like to make a query for all ContentItem 's that are of a particular account, and are either created by the logged in user or are public. Here's the query I wrote query = ContentItem.objects.filter( (Q(account=account) & Q(public=True)) | (Q(account=account) & Q

Mongoengine: TypeError: __init__() got an unexpected keyword argument

不打扰是莪最后的温柔 提交于 2019-11-29 11:31:18
I am using flask-mongoengine extension and I have a User class like this: class User(db.Document, UserMixin): email = db.StringField(max_length=120, required=True, unique=True) password_hash = db.StringField(max_length=80, required=True) active = db.BooleanField() fb_id = db.StringField(max_length=120, required=False) def __init__(self, email, password, fb_id=None, active=True): hashp = md5.md5(password).hexdigest() self.email=email self.password_hash=hashp self.fb_id=fb_id self.active=active But when I do a simple get: User.objects.get(email = email) I get the error: TypeError: __init__() got

How to get ReferenceField data in mongoengine?

时间秒杀一切 提交于 2019-11-29 10:58:50
I'm have a problem that query set retrieving oid in json, and I would like to retrieve actual username of that User collection I have below: class User(db.Document): username = db.StringField(required=True) password_hash = db.StringField() is_admin = db.IntField(default=0) class Message(db.EmbeddedDocument): to_users = db.ListField(db.ReferenceField(User)) created_at = db.DateTimeField(default=datetime.now) is_read = db.BooleanField(default=False) body = db.StringField(required=True) class Inbox(db.Document): from_user = db.ReferenceField(User, required=True) subject = db.StringField(max

Mongoengine: ConnectionError: You have not defined a default connection

99封情书 提交于 2019-11-29 10:42:32
In my new Django project I set up a MongoDb database and use mongoengine module but I can't properly access to dabase nore in shell no in views. "ConnectionError: You have not defined a default connection" My settings.py includes the following: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DaTaBaSe', 'USER': 'root', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '3306', }, 'tracking': { 'ENGINE': 'django.db.backends.dummy', 'NAME': 'analytics', } } import mongoengine SESSION_ENGINE = 'mongoengine.django.sessions' mongoengine.connect(_MONGODB_NAME, 'localhost:27017')

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

大兔子大兔子 提交于 2019-11-28 21:17:20
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? Note that get_or_create is now scheduled to be deprecated, because with no transaction support in MongoDB it cannot ensure atomicity. The

Sort using MongoEngine?

。_饼干妹妹 提交于 2019-11-28 21:08:44
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 dcrosta 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 = StringField() last_name = StringField() age = IntField() # later people = Person.objects.order_by