mongoengine

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

喜你入骨 提交于 2019-12-02 01:23:45
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' ) You could add multiple databases for your app in your settings.py like, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'admin_db

Connecting to multiple mongodb instances from django

南笙酒味 提交于 2019-12-02 00:13:38
I am using mongoengine with Django and within my project need to connect to two instances of MongoDB while serving single request. It works just fine if I use: connect("mdb1") #do stuff with mdb1 ... connect("mdb2") #do stuff with mdb2 but I am wondering if that's a proper way of doing it. Matt Zeunert Multiple database support was added in MongoEngine 0.6 Demo using register_connection . alias_lists = ['users-books-db', 'user-db', 'book-db'] # list of aliases dbs = ['author-book-pairs', 'users', 'books'] # list of databases for alias, db in zip(alias_lists, dbs): register_connection(alias, db

Connecting to multiple mongodb instances from django

谁都会走 提交于 2019-12-01 22:19:19
问题 I am using mongoengine with Django and within my project need to connect to two instances of MongoDB while serving single request. It works just fine if I use: connect("mdb1") #do stuff with mdb1 ... connect("mdb2") #do stuff with mdb2 but I am wondering if that's a proper way of doing it. 回答1: Multiple database support was added in MongoEngine 0.6 Demo using register_connection. alias_lists = ['users-books-db', 'user-db', 'book-db'] # list of aliases dbs = ['author-book-pairs', 'users',

multi document insert using mongoengine into mongodb

我与影子孤独终老i 提交于 2019-12-01 02:16:44
In my flask app I am using MongoeEgine. I am trying to insert multiple documents into my places collection in my MongoDB. My document class is defined as class places(db.Document): name = db.StringField(max_length=200, required=True) loc = db.GeoPointField(required=True) def __unicode__(self): return self.name a=[] a.append({"name" : 'test' , "loc":[-87,101]}) a.append({"name" : 'test' , "loc":[-88,101]}) x= places(a) The last statement fails x= places(a) TypeError: __init__() takes exactly 1 argument (2 given) I also tried to save this to my instance places.insert(x) places.save(x) both fail.

Querying a list in mongoengine; contains vs in

假装没事ソ 提交于 2019-11-30 22:59:25
I have a ListField in a model with ids (ReferenceField), and I need to do a query if a certain id is in that list. AFAIK I have 2 options for this: Model.objects.filter(refs__contains='59633cad9d4bc6543aab2f39') or: Model.objects.filter(refs__in=['59633cad9d4bc6543aab2f39']) Which one is the most efficient for this use case? The model looks like: class Model(mongoengine.Document): refs = mongoengine.ListField(mongoengine.ReferenceField(SomeOtherModel)) From what I can read in the mongoengine documentation, http://docs.mongoengine.org/guide/querying.html#string-queries , contains is really a

Use pymongo in django directly

↘锁芯ラ 提交于 2019-11-30 16:38:41
I am building a website using Django and MongoDB. There are 2 popular API framework that we can use to connect Django and MongoDB , one is mongoengine and the other is django-mongodb-engine . Because the latest mongoengine is not supported Django anymore Document , and django-mongodb-engine needs another django-nonrel package which makes the development environment a little bit complicate. I am wondering, if I could use Pymongo to connect Django and MongoDB directly. Is there anyone who have the same experience that could share? and how to set the db in setting.py in Django to make the db

Use pymongo in django directly

不打扰是莪最后的温柔 提交于 2019-11-30 16:20:06
问题 I am building a website using Django and MongoDB. There are 2 popular API framework that we can use to connect Django and MongoDB , one is mongoengine and the other is django-mongodb-engine . Because the latest mongoengine is not supported Django anymore Document, and django-mongodb-engine needs another django-nonrel package which makes the development environment a little bit complicate. I am wondering, if I could use Pymongo to connect Django and MongoDB directly. Is there anyone who have

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

那年仲夏 提交于 2019-11-30 15:13:46
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 it? This is actually covered in the unit tests if not clear from the documentation itself: class Post

MongoDB: Removing a field from ALL subdocuments in an array field

早过忘川 提交于 2019-11-30 14:55:36
I have thousands of documents in this format: { "_id" : ObjectId("51e98d196b01c2085c72d731"), "messages" : [ { "_id" : ObjectId("520167056b01c20bb9eee987"), "id" : ObjectId("520167056b01c20bb9eee987"), }, { "_id" : ObjectId("520167056b01c20bb9eee988"), "id" : ObjectId("520167056b01c20bb9eee988"), }, { "_id" : ObjectId("520167056b01c20bb9eee989"), "id" : ObjectId("520167056b01c20bb9eee989"), } ], } I need to remove the duplicate "id" field. This is what I have tried: db.forum_threads.update({}, {$unset: {"messages.$.id": 1}}, {multi: true}); This is the error I am getting: Cannot apply the

What is the difference between EmbeddedDocumentField and ReferenceField in mongoengine

夙愿已清 提交于 2019-11-30 14:54:00
问题 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