How do I update embedded documents in Doctrine MongoDB? How do I update embedded documents in Doctrine MongoDB? mongodb mongodb

How do I update embedded documents in Doctrine MongoDB?


I faced exactly the same issue. It turns out the UnitOfWork seems to fail in computing the changesets of documents with other documents embedded, though I have not been able to figure out why...As a result, when when i compare the actual value and the original one, unit of work shows the same value for both. Speaking with your Variant A, when you

$document->getDocB()->setValueB('foo baz');

Unit of work shows "foo baz" for both the old and the new value and will not recognize it as a a change and will therefor not update it.

Anyway, this leeds to a workaround:

$document = ... // find from data store$document->setValueA('Hello World');$docB = $document->getDocB();$docB->setValueB('foo baz');$om->detach($docB);$om->persist($document);$om->flush();

This makes the unit of work recognize the docB of $document as a newly set document and will flush it as expected.


MongoDB has only atomic operations.You have options:1. Query the document, find the apropriate subdocument, update the whole document or its part.Pros: easy logicCons: non-atomic2. Use positional $ operator is your subdocuments are in list.