3

I am doing web development in Java with MongoDB on the backend. I have two database objects that need to have lists of each other. ex-

The Customer object contains - List<MediaServer>

The MediaService object contains - List<Customer>

1) Should I remove one of the lists so the same information is not stored in two places? e.g. Remove the customer list of media servers. When I pull a customer, iterate through all media servers to figure out what media servers the customer is assigned to.

2) Is there a better place to store this style of information in MongoDB?

Thanks!

mkeathley
  • 259
  • 2
  • 9
  • Can't you store the references as a list of ids and retrieve the real objects when needed ? That way, there is no duplicate and it's easy to retrieve the references. – Nicolas Morel Jun 02 '13 at 08:47
  • @NicolasMorel Yes, I should have mentioned that the list's are simply ids (dbRefs in MongoDB). The duplication is not object duplication but relationship duplication- i.e. the relationship between customer and mediaserver is in both lists. – mkeathley Jun 02 '13 at 19:33

2 Answers2

1

In MongoDB it's much more normal to have data duplicated in multiple places. If your lists are not changing frequently, there's no real downside to storing both lists - hard drive space is not expensive these days like it was when normalization was all the rage.

The thing to always bear in mind with Mongo (and with any application development) is to figure out what your access patterns are and to create your schema based on the frequent patterns. If you are reading these lists regularly but not updating them very much, then having both lists in the DB will be fine.

If, however, you're frequently changing both lists, you might find yourself updating the data in two places every time.

However, you should also remember that if you're storing lists of IDs, when you want to get the full Object/Document for this ID it will require a round trip to the server. So if for every customer (for example) you always need all the info about all the MediaServers, you will need to make a trip over the network for every one of the MediaServer IDs in your list for that customer.

Trisha
  • 3,891
  • 1
  • 25
  • 39
0

I suggest you use Soft References for one kind of lists, and regular references for the other. Alternatively, if the existance of client and server records is 'hooked' onto other referecning objects, maybe both kinds of lists should hold soft references.

Weak references might also be worth considering. If memory use is an issue. See What is the difference between a soft reference and a weak reference in Java? about that.

PS - My suggestion has nothing to do with the fact that your objects originate in a MongoDB or any other kind of database.

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684