Can DBRefs contain additional fields?

I came across several situations when using MongoDB that require the use of DBRefs. However, I would also like to cache some fields from the referenced document in the DBRef itself.

{$ref:'user', $id:'10285102912A', username:'Soviut'} 

For example, I can have a username available even if it refers to a user document. This would provide me all the benefits of a single approach to the document; Speeding up queries and eliminating the need to do manual dereferencing in my code. At the same time, letting me use links where they make sense.

The idea is that when updating a reference document (for example, the user changes his name), my business layer can automatically update all documents that link to it.

Ultimately, I wonder if he thinks a good form for storing extra fields in my DBRefs? Will it break anything? Can I lose my data every time the link is rewritten? Will drivers like pymongo support him?

+4
source share
1 answer

Ultimately, I wonder if he thinks a good form for storing extra fields in my DBRefs?

It may be that the pure ones have separate cached and ref fields ... it depends on what your data is.

Will I lose my data every time the link is overwritten?

You could, but not careful. If you are updating a DB subobject, just make sure that you are updating certain fields that you want to update, rather than overwriting the entire subobject.

Remember that links are ordinary objects. A database reference is a standard, not a special type.

Will drivers like a pimongo support him?

Driver dereferencing assistants will still work. Helpers just do findOne with the $ ref and $ id fields.

+3
source

Source: https://habr.com/ru/post/1310688/


All Articles