mongoengine

mongoengine connection and multiple databases

这一生的挚爱 提交于 2019-12-19 10:10:12
问题 I have 2 databases I want to query from, but I only get results from one. I'm using mongoengine with python and graphene (it's my first time). I've exhausted my search and I don't understand how I can resolve this issue. Here is my code: import graphene from mongoengine import Document, connect from mongoengine.context_managers import switch_collection from mongoengine.fields import ( StringField, UUIDField, IntField, FloatField, BooleanField, ) from graphene_mongo import

multi document insert using mongoengine into mongodb

别等时光非礼了梦想. 提交于 2019-12-19 05:06:12
问题 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

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

拟墨画扇 提交于 2019-12-18 16:56:25
问题 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({}

Get required fields from Document in mongoengine?

馋奶兔 提交于 2019-12-18 12:28:19
问题 I want to be able to get a list or some sort of set of fields that are required by the document i've created. For instance, here is my document: nickName = StringField(required=True) password = StringField(required=True) firstName = StringField() lastName = StringField() joinDate = DateTimeField(required=True) lastVisited = DateTimeField(required=True) subscriptions = DictField(field=ObjectIdField()) isActivated = BooleanField(default=True) profileImage = FileField() isModerator =

mongoengine - Query on ListField of EmbeddedDocumentField

安稳与你 提交于 2019-12-17 21:14:52
问题 I use mongoengine with Django and python. This is my code: class Chambre(EmbeddedDocument): max_personne = IntField(default=0) prix = IntField(default=0) class Hotel(Document): code = IntField(default=0) nom = StringField(max_length=200) chambre = ListField(EmbeddedDocumentField(Chambre)) resume = StringField(max_length=200) 1 - I want a query to filter all Hotel that have at least a Chambre with prix >= a (a floeat number) 2 - also have that Chambre Any idea? 回答1: You can use the embedded

Can't Return JSON object using MongoEngine Pymongo with Django?

北慕城南 提交于 2019-12-14 00:22:37
问题 So I'm trying to return a JSON object for a project. I've spent a few hours trying to get Django just returning the JSON. Heres the view that we've been working with: def json(request, first_name): user = User.objects.all() #user = User.objects.all().values() result = simplejson.dumps(user, default=json_util.default) return HttpResponse(result) Here's my model: class User(Document): gender = StringField( choices=['male', 'female', 'Unknown']) age = IntField() email = EmailField() display_name

NotUniqueError: Tried to save duplicate unique keys

妖精的绣舞 提交于 2019-12-13 16:18:43
问题 I keep getting the above mentioned error. I have deleted the existing db field id which was set to unique. But on save I got the below exception raise NotUniqueError(message % unicode(err)) NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: test.users.$id_1 dup key: { : null }) My user table looks like, class Users(db.Document, UserMixin): name = db.StringField(max_length=50) email = db.StringField(max_length=255) password = db.StringField(max_length=255)

mongoDB references fetching takes time

柔情痞子 提交于 2019-12-13 06:26:22
问题 I use mongoengine as Object-Document mapper. Here is a brief description of the collections that are causing the problem. Each document in collection A, can have a list of references to documents in Collection B. class A(Document): list_b = ListField(EmbeddedDocumentField(EB)) #other fields are not mentioned. class EB(EmbeddedDocument): b_reference = ReferenceField('B') loc = GeoPointField() class B(Document): name = StringField() #other fields are not mentioned. When i try to access the list

how to switch database name in mongoengine

二次信任 提交于 2019-12-13 03:01:46
问题 I have multiple databases with same collection names, document types, etc. In source code at Github, documents use get_db method to choose the database to work on and in that method there is another call to get server connection. Both of these processes works with same parameter that called "alias". Let's say that I have all my db connections defined with same server details and different databases, aliases. In that way, there will be multiple connections to same server. But it would be

Server side generated id

心不动则不痛 提交于 2019-12-13 01:42:57
问题 The docs say: Usually, the id will be generated automatically by the database server when the object is save, meaning that you may only access the id field once a document has been saved: >>> page = Page(title="Test Page") >>> page.id >>> page.save() >>> page.id ObjectId('123456789abcdef000000000') But I have found that it's not true: ipdb> > .../lib/python2.7/site-packages/pymongo/collection.py(386)gen() 385 if '_id' not in doc: --> 386 doc['_id'] = ObjectId() 387 So pymongo generates id on