We’ve designed our Mongo database to be highly denormalized, resulting in many documents in our collections containing very large arrays as some of the fields. Naturally, this can result in downloads from our DB longer than necessary because the documents are just so large.
Whenever we need to grab some records from the DB, I have mitigated the performance implications of this by using .only to choose just the fields I want, but this requires me to download that extra data before I may need it, and in general it’s a lot more involved for me to keep track of what fields end up being needed when I am querying for the document(s).
Does Mongoid have a way that I can simply define particular fields in my model as ones that should be lazily loaded so that I grab them from the server just when they’re first accessed? I searched through Mongoid’s documentation to see if it had anything built in, but I’m not seeing any such thing. Perhaps there’s a third party gem that adds this functionality to Mongoid?
Mongoid doesn’t have support for lazy loading data from the server, not aware of any plugins to do it either.
While technically you could add this to Mongoid, you would still be better off manually specifying
onlyso you load what you need once. If you lazy loaded the fields based on usage, you would have to pull the data from MongoDB every time a field is accessed while nil.Meaning if you accessed 5 different fields on top of the original document load, you’re sending 6 queries to MongoDB which involves the general roundtrip/processing as compared to just specifying it in
onlyin the first place.