Mongoengine - what is a link store

In mongoengine, what value should be set in the ReferenceField. I mean, we have to provide in the "ObjectId" of the document to which the link should be made. For example,

class Bar(Document): content = StringField() foo = ReferenceField('Foo') 

An object of class Bar must have the value set in the attribute "foo". Should it be the ObjectId of any document in the Foo collection? You can also set any other unique field as a value in the link field, which indicates which field it is in?

+7
source share
2 answers

It stores a DBRef, you just need to pass an instance of Foo, and it will be converted automatically. See the section in the docs: http://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html?highlight=referencefield

+3
source

Prior to version 0.8, MongoEngine defaults to storing DBRef. For 0.8 and later, it saves the ObjectId by default.

There is a dbref parameter that should be used when creating a ReferenceField ( explicit is better than implicit ):

 class Bar(Document): content = StringField() foo = ReferenceField('Foo', dbref = True) # will use a DBRef bar = ReferenceField('Bar', dbref = False) # will use an ObjectId 

Here is the documentation for ReferenceField .

I have version 0.7.9 installed, and when I create a ReferenceField without the dbref parameter, I get the following warning:

 [...]/lib/python2.7/site-packages/mongoengine/fields.py:744: FutureWarning: ReferenceFields will default to using ObjectId strings in 0.8, set DBRef=True if this isn't desired warnings.warn(msg, FutureWarning) 
+11
source

All Articles