3

Let's assume I have mongoose models for books and pages like this:

mongoose.model("Book", new Schema({
    title: String
});

and this

mongoose.model("Page", new Schema({
    pageNumber: Number,
    _bookId: {type: ObjectId, ref: "Book"}
});

Every page keeps track which book it belongs to. Now I want to have an array of books that have a page with pageNumber 500.

I could do the following:

Page.find({pageNumber: 500})
    .populate("_bookId")
    .then(function (pages) {
        var books = [];
        pages.forEach(function (page) {
            books.push(page._bookId); // page._bookId now contains a Book document
        });
        return q(books);
    }).then(function (books) {
        // Do something with the books
    });

Yet, the part where I loop over the pages seems cumbersome and that kind of extraction could probably be done by mongo. My question is how that would work.

Is using populate even the best way to go here? I would like to keep the schemas the way they are though.

Florian
  • 712
  • 5
  • 18

1 Answers1

1

I think your schema design is the issue here. Why is pages a separate schema? You should use Mongo's embedding capabilities to make Page an array in Book:

mongoose.model("Book", new Schema({
    title: String,
    pages: [...]
});

Then you can search for books that have a page #N.

Additionally, if your pages is nothing more than a page number and an associated book you can just make pages a number representing the total number of pages.

Edit: If such a schema is just a simplification of your use case and really you cant do embedding, then you're out of luck. The abstraction your looking for is called a join, and Mongo doesn't support that because its not what Mongo is going for. If thats really a primary use case of yours you should look to using a relational database (or change your schema).

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • Thanks for your answer. The above is just an example, in reality I'm using much more complex schemas. There are reasons why I'm not using embedded schemas, but here I'm looking for a better way to deal with this kind of schema. If not being able to do the above in a concise way is an inherent restriction of the schema design that's fine, I'm just wondering if that is true. – Florian Nov 24 '15 at 14:55
  • 1
    @Florian In that case you're out of luck. The abstraction your looking for is called a join, and Mongo doesn't support that because its not what Mongo is going for. If thats really a primary use case of yours you should look to using a relational database (or change your schema). – David says Reinstate Monica Nov 24 '15 at 15:23