Using subdocument _id fields for multi-environment support

Stuart Bishop stuart.bishop at canonical.com
Wed Oct 1 12:38:35 UTC 2014


On 1 October 2014 11:25, Menno Smits <menno.smits at canonical.com> wrote:

> MongoDB allows the _id field to be a subdocument so Tim asked me to
> experiment with this to see if it might be a cleaner way to approach the
> multi-environment conversion before we update any more collections. The code
> for these experiments can be found here:
> https://gist.github.com/mjs/2959bb3e90a8d4e7db50 (I've included the output
> as a comment on the gist).
>
> What I've found suggests that using a subdocument for the _id is a better
> way forward. This approach means that each field value is only stored once
> so there's no chance of the document key being out of sync with other fields
> and there's no unnecessary redundancy in the amount of data being stored.
> The fields in the _id subdocument are easy to access individually and can be
> queried separately if required. It is also possible to create indexes on
> specific fields in the _id subdocument if necessary for performance reasons.

Using a subdocument for the _id is taught and recommended in the
MongoDB courseware. In particular, the index is more useful to the
query planner. If the fields are separate, then mongodb will end up
querying by unit name and then filtering the results by environment
(but that won't matter much in this case).

-- 
Stuart Bishop <stuart.bishop at canonical.com>



More information about the Juju-dev mailing list