Novice Question: Copy or Link Data?

26 views
Skip to first unread message

David Striga

unread,
Jun 11, 2016, 9:52:11 AM6/11/16
to mongodb-user
I am fairly new to MongoDB, so I apologize if the answer to this question is obvious.

In my app, I have lists and items in the list. A user can click an item in the list and have it added to their own list. Should I copy each field that I need from the item in the list to the user's item in their list, or should I just copy the ID of the item to the user's item? If I just copy the ID, I'll have to query the database for each item I load in the user's list. What do you think I should do in this situation? Right now, I am thinking I should only save the data that I will immediately show the user to the user's list item and show a button for more info if they want it. That way, I will not have to make an extra query.

Thank you, in advance for your help.

Chris Cunningham

unread,
Jun 28, 2016, 1:44:03 AM6/28/16
to mongodb-user

David,

There are benefits and drawbacks to either method and it is dependant on the application usage of that data to determine which method is best.

bq. “I am thinking I should only save the data that I will immediately show the user to the user’s list item and show a button for more info if they want it.”

In a use case where only a small amount of data will be displayed to the user at a time, then perhaps using references would be more efficient as it would not require all data to be displayed, only a small portion. Again, it will vary from case to case depending on data usage.

If the data is copied from one collection and embedded into another collection, it has an advantage in that Embedded Data Model allows the data to be fetch in a single operation.

However, you need to consider the BSON document size limit when embedding large data. You would also need to consider data integrity when modifying data that lives in two locations.

You may find the documentation on Operational Factors helpful in explaining Data Growth and Atomicity within MongoDB. Please see Operational Factors for more information.

If you are copying only the ID from one collection to another, known as referencing, it has an advantage in that References Data Model maintains better data integrity when data is modified. However, for most frequently accessed data it may not be as efficient, as you may have to issue follow-up queries to resolve the references and retrieve the rest of the data.

You can find more information on this in the Data Model Design documentation.

Chris

Reply all
Reply to author
Forward
0 new messages