Valentin H
Valentin H

Reputation: 7458

Understanding MongoDB references

I read about data-models in MongoDB and came to one to many relationship:

In the following example, does it mean the book-documents should be in the collection books and MongoDB will somehow link the documents with ids matching the array books to the publisher? Or does the link exists only in the architector's head?

{
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA",
   books: [12346789, 234567890, ...]
}

{
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: [ "Kristina Chodorow", "Mike Dirolf" ],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English"
}

{
   _id: 234567890,
   title: "50 Tips and Tricks for MongoDB Developer",
   author: "Kristina Chodorow",
   published_date: ISODate("2011-05-06"),
   pages: 68,
   language: "English"
}

Upvotes: 2

Views: 351

Answers (3)

Frederick Motte
Frederick Motte

Reputation: 1164

In MongoDB itself, it is not possible as far as I know, but in MongooseJS, the javascript wrapper around MongoDB it is possible by saving the reference to the book._id in the publisher.books array.

var publisherSchema = Schema({
  _id     : Number,
  name    : String,
  founded     : Number,
  books : [{ type: Schema.Types.ObjectId, ref: 'Book' }]
});

var bookSchema = Schema({
  _id     : Number,
  title    : String,
  author    : [{type: String}],
  pages: Number,
  language: String
});

var Book  = mongoose.model('Book', bookSchema);
var Publisher = mongoose.model('Publisher', publisherSchema);

When querying the data, you can then use the populate method.

Publisher.
    find().
    populate('books')

Upvotes: 1

SiddAjmera
SiddAjmera

Reputation: 39482

MongoDB isn't going to somehow link the documents with ids matching the array books to the publisher. So yes the link exists only in the architect's head

The article is simply trying to explain how a one to many relationship can be formulated between documents in MongoDB.

One to many relationship in MongoDB is also further classified as one to few and one to squillions. If the scenario is one to few, an approach like this can be used:

{
    name: "O'Reilly Media",
    founded: 1980,
    location: "CA",
    books: [12346789, 234567890, ...]
} {
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: ["Kristina Chodorow", "Mike Dirolf"],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English"
} {
    _id: 234567890,
    title: "50 Tips and Tricks for MongoDB Developer",
    author: "Kristina Chodorow",
    published_date: ISODate("2011-05-06"),
    pages: 68,
    language: "English"
}

But in real a publisher could have millions of books published. So in that case it would be an example of one to squillions. Now storing millions of book_ids may make a MongoDB Document exceed the size of 16MB which is limit to the size a MongoDB Document can have. So in that case it's better to use this approach:

{
    _id: "oreilly",
    name: "O'Reilly Media",
    founded: 1980,
    location: "CA"
} {
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: ["Kristina Chodorow", "Mike Dirolf"],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English",
    publisher_id: "oreilly"
} {
    _id: 234567890,
    title: "50 Tips and Tricks for MongoDB Developer",
    author: "Kristina Chodorow",
    published_date: ISODate("2011-05-06"),
    pages: 68,
    language: "English",
    publisher_id: "oreilly"
}

Upvotes: 3

vesse
vesse

Reputation: 5078

Those are called manual references in MongoDB documentation and are the preferred method for relations. There is also DBRef, but as the documentation states you shoud use manual references:

In most cases you should use the manual reference method for connecting two or more related documents. However, if you need to reference documents from multiple collections, consider using DBRefs.

Neither method really links anything in the DB level.

Upvotes: 2

Related Questions