I designed the schemas much like a relational database. Like so:
User
user_id: String
...
Book
user_id: String
book_id: String
...
Page
book_id: String
page_id: String
...
Because a User can have many many books, and a Book has many pages.
Now I want to query the first 5 pages of the first 25 books of a user. How would I do that in MongoDB?
Well, MongoDB doesn’t really have ‘relational’ features like MySQL/etc have. So you need to fetch the data using multiple queries by first getting your user’s id, then finding books like
db.books.find({ "user_id": uid }).limit(25), then using that to go through each book to similarly find the first 5 pages of each.Alternatively you could embed the pages into the book, but then we get into a whole other story about how to optimize the querying 🙂