mongodb - How to avoid loading referenced documents when creating a new document? -
i have playlist
document references many song
documents, in turn reference others documents:
/** @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; }
another document like
references 1 playlist
:
/** @document(collection="likes") */ class { /** * @var playlist * * @referenceone(targetdocument="playlist", simple=true) */ protected $playlist; }
each time persist (insert) new like
document, of references (deep or not) loaded (a lot of associated queries executed) ; is, in specific case, useless.
how can avoid "greedy" behavior (model refactor, configuration, caching etc.)?
use manual referencing:
https://docs.mongodb.org/manual/reference/database-references/#manual-references
in short store "list"
id in articles
it's associated with.
then when change "list"
not need load associated articles.
you can articles given "list" still with:
db.articles.find({"listid" : "foo"})
i think purpose of "list"
collection change - unsure without seeing schema. reason quotes around "list"
.
Comments
Post a Comment