2016-02-05 62 views
0

我有一个Playlist文件,它引用了许多Song文件,这反过来又引用他人的文档:如何避免在创建新文档时加载参考文档?

/** @Document(collection="playlists") */ 
class Playlist 
{ 
    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ReferenceMany(targetDocument="Song", simple=true) 
    */ 
    protected $songs; 
} 

/** @Document(collection="songs") */ 
class Song 
{ 
    /** 
    * @var string 
    * 
    * @ReferenceOne(targetDocument="Foo", simple=true) 
    */ 
    protected $foo; 

    /** 
    * @var string 
    * 
    * @ReferenceOne(targetDocument="Bar", simple=true) 
    */ 
    protected $bar; 
} 

另一份文件Like引用一个Playlist

/** @Document(collection="likes") */ 
class Like 
{ 
    /** 
    * @var Playlist 
    * 
    * @ReferenceOne(targetDocument="Playlist", simple=true) 
    */ 
    protected $playlist; 
} 

每次我坚持(插入)新Like文档中,加载了一些引用(很深或很深)(执行了很多相关的查询);在这种情况下,这是无用的。

如何避免这种“贪婪”行为(模型重构,配置,缓存等)?

回答

1

使用说明书引用:

https://docs.mongodb.org/manual/reference/database-references/#manual-references

总之商店"List" ID在Articles与相关联。

然后,当您更改"List"时,不需要加载所有关联的文章。

你可以得到所有文章对于给定的“单子”仍在:

db.articles.find({"listId" : "foo"})

我觉得"List"收集的目的将发生变化 - 不确定,没有看到您的架构。这是在"List"附近引用的原因。