引用现有文档的ReferenceField [英] ReferenceField with reference to existing document

查看:72
本文介绍了引用现有文档的ReferenceField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解, mongoengine的ReferenceField 允许将引用传递给存储在数据库中的另一个文档.

As far as I understood, mongoengine's ReferenceField allows to pass a reference to another document stored in the DB.

到目前为止,我已经能够在不同的集合中创建多个文档(在本示例中减少为两个),现在我需要互相引用.

So far I was able to create several documents in different collections (reduced to two in this example), which I need to reference to each other now.

正如您将在下面看到的那样,当我在regenerators集合中创建新文档时,我想传递给定材料的引用.在我提供的示例中,这意味着将引用传递给文档 aisi304 .

As you will see below I want to pass reference of a given material when creating a new document in the regenerators collection. In the example I provided, this means to pass a reference to the document aisi304.

我的再生器文档定义如下:

My regenerator documents are defined as follows:

class Regenerators(Document):
    material = ReferenceField(Materials, required=True)

物料文档需要引用到再生器文档,其定义如下:

The materials document needs to be referenced to a regenerator document and is defined like this:

class Materials(Document):
    title = StringField(unique=True, required=True)

但是,在这种情况下,所需的物料文档已经存储在DB中:

However, the desired materials document is stored in the DB already as in this case:

{
    "_id": ObjectId("565b84dc55c40f63392ffdee"),
    "title": "aisi304"
}

因此,我尝试创建一个新的再生器文档,如下所示,传递要作为关键字参数引用的材料文档的标题:

So I tried to create a new regenerator document as follows passing the title of the material document to be referenced as a keyword argument:

# the passed keywords are read from json normally, but I put it in directly for the sake of readability
r = Regenerators(material="aisi304")
r.save()

但是,由于数据库中存储的注册人文档看起来像这样(使用mongod进行调试),因此引用似乎未正确传递:

However, the reference does not seem to be passed correctly since the regernator document stored in the DB looks like this (using mongod for debugging):

{
    "_id": ObjectId("565b89d355c40f6355fa5f45"),
    "material": "aisi304"
}

我认为我会实现这样的目标:

I thought I would achieve something like this:

{
    "_id": ObjectId("565b89d355c40f6355fa5f45"),
    "material": {
        "_id": ObjectId("565b84dc55c40f63392ffdee"),
        "title": "aisi304"
    }
}

在文档中提供的教程中传递对新创建的文档的引用.但是,这不适合我,因为我需要参考现有文档.

In the tutorial provided in the docs they pass a reference to a newly created document. However, this is not suitable for me since I need to reference to existing documents.

我在做什么错了?

推荐答案

您做错了.您应该保存对您的 Materials 对象的引用,然后将其作为参数传递给 Regenerators

You are doing it wrong. You shoud save a reference to your Materials object then pass it as argument to Regenerators

演示

class Materials(Document):
    title = StringField(unique=True, required=True)

class Regenerators(Document):
    material = ReferenceField(Materials, required=True)

m = Materials(title='aisi304').save()

r = Regenerators(material=m).save()

然后您的文档如下所示:

Then your documents look like this:

> db.regenerators.find()
{
        "_id" : ObjectId("565c9d110acf4510cf1f8712"),
        "material" : ObjectId("565c9cfc0acf4510cf1f8711")
}
> db.materials.find()
{ "_id" : ObjectId("565c9cfc0acf4510cf1f8711"), "title" : "aisi304" }
> 

如果要使用对现有文档的引用,则需要使用 .get 方法,然后将其引用作为参数传递给 Regenerators

If you want to use a reference to an existing document you need to issue a query using the .get method then pass it reference as argument to Regenerators

m = Materials.objects.get(title='aisi304')
r = Regenerators(material=m).save()

这篇关于引用现有文档的ReferenceField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆