Mongoengine Link to Existing Collection
Asked Answered
C

2

5

I'm working with Flask/Mongoengine-MongoDB for my latest web application.

I'm familiar with Pymongo, but I'm new to object-document mappers like Mongoengine.

I have a database and collection set up already, and I basically just want to query it and return the corresponding object. Here's a look at my models.py...

from app import db

# ----------------------------------------
# Taking steps towards a working backend.
# ----------------------------------------

class Property(db.Document):

    # Document variables.
    total_annual_rates = db.IntField()
    land_value = db.IntField()
    land_area = db.IntField()
    assessment_number = db.StringField(max_length=255, required=True)
    address =  db.StringField(max_length=255, required=True)
    current_capital_value = db.IntField
    valuation_as_at_date = db.StringField(max_length=255, required=True)
    legal_description = db.StringField(max_length=255, required=True)
    capital_value = db.IntField()
    annual_value = db.StringField(max_length=255, required=True)
    certificate_of_title_number = db.StringField(max_length=255, required=True)

    def __repr__(self):
        return address

    def get_property_from_db(self, query_string):
        if not query_string:
            raise ValueError()
        # Ultra-simple search for the moment.
        properties_found = Property.objects(address=query_string)
        return properties_found[0]

The error I get is as follows: IndexError: no such item for Cursor instance

This makes complete sense, since the object isn't pointing at any collection. Despite trolling through the docs for a while, I still have no idea how to do this.

Do any of you know how I could appropriately link up my Property class to my already extant database and collection?

Countermove answered 23/8, 2014 at 22:35 Comment(0)
C
6

The way to link a class to an existing collection can be accomplished as such, using meta:

class Person(db.DynamicDocument):

    # Meta variables.
    meta = {
        'collection': 'properties'
    }

    # Document variables.
    name = db.StringField()
    age = db.IntField()

Then, when using the class object, one can actually make use of this functionality as might be expected with MongoEngine:

desired_documents = Person.objects(name="John Smith")
john = desired_documents[0]

Or something similar :) Hope this helps!

Countermove answered 26/8, 2014 at 21:9 Comment(0)
G
3

I was googling this same question and i noticed the answer has changed since the previous answer:

According to the latest Mongoengine guide:

If you need to change the name of the collection (e.g. to use MongoEngine with an existing database), then create a class dictionary attribute called meta on your document, and set collection to the name of the collection that you want your document class to use:

class Page(Document):
meta = {'collection': 'cmsPage'}

The code on the grey did the trick and i could use my data instantly.

Getup answered 19/7, 2018 at 18:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.