-1

This is a data model problem that I have. Lets say I have a 2 docs: Publishers and Books. If my publisher can publish many books, how do I decide if I want to embed books in the publisher or publishers in books?

I'm not asking if I should embed vs reference, I'm asking the direction of the embedding. When to use which direction.

mskw
  • 10,063
  • 9
  • 42
  • 64

1 Answers1

0

As always, 'it depends'

Depends on your queries -

If you are going to be searching primarily on Book I would suggest having a collection of Books, with an embedded publisher.

Assuming a schema like

Book

{
    _id: ObjectId('507f191e810c19729de860ea'),
    title: 'A great book',
    publisher: {
        _id: ObjectId('958d191e810c19629dl860io'),
        name: 'A great publisher'
    }
}

You could easily query this like:

db.books.find({publisher._id: ObjectId("958d191e810c19629dl860io")});

Which will return all books by that publisher.

The issue I see is around how much you want to 'present' about the publisher to the person viewing a book. Could this data change? The publisher could change their name? Then you've got to go through each book and update it... Or would you prefer to store it as a 'point in time fact' - ie, this book was published by a publisher with xyz name, therefore we don't update it.

If you're primary query however is a list of publishers, and their books, I would reverse the above:

Publishers

{
    _id: ObjectId('958d191e810c19629dl860io'),
    title: 'A great publisher',
    books: [{
        _id: ObjectId('507f191e810c19729de860ea'),
        name: 'A great book'
    }]
}
Alex
  • 37,502
  • 51
  • 204
  • 332