2

For a library, I need to keep track of users and books. Basically I need to be able to know:

  • the list of books currently borrowed by a user
  • the current borrower of a given book

The app is done with node.js and mongoDB (with moogoose). I have the following schema:

BookSchema = new Schema({
  title        : String,
  author : String,
  current_borrower_email: String,
});
mongoose.model('Book', BookSchema);

// Define User model
UserSchema = new Schema({
  lastname  : String,
  firstname  : String,
  email     : String,
  books    : [BookSchema]  // Books the user is borrowing
});
mongoose.model('User', UserSchema);

I guess this would be simplier to set this up in a relational DB where I could easily use many to many relation ships with foreign keys but I wanted to give a try to MongoDB.

Do you think this solution could work ? Also, if I delete a Book object, it seems I will have to remove it manually from the array of the user who borrowed it, it that right ?

Kamil Gosciminski
  • 16,547
  • 8
  • 49
  • 72
Luc
  • 16,604
  • 34
  • 121
  • 183
  • regarding the cascading upon deletion : see http://stackoverflow.com/questions/3115261/mongodb-dbref-on-delete-cascade for reference – Winfred Apr 15 '11 at 07:26
  • If you like the many-to-many relation implementation ib relational DB, you can mimic it in mongodb with an additional table called `borrowing`, which relates user and book. btw, your current implementation doesn't keep record of borrowed books that returned? I think Your current schema wouldnt work in real life. – Winfred Apr 15 '11 at 07:29
  • you'r right, it's not taken into account currently, this is just the basis of the schema. I just study the thing without going to far, just to see if NoSQL is a good candidate for my needs. You'r right, I'll probably go for an additionnal table. – Luc Apr 15 '11 at 07:41
  • @Luc: Why many to many? With schema provided above seems only one user can take some particular book. I've missed something? it's one to many replation.. – Andrew Orsich Apr 15 '11 at 07:45
  • in the DB world, most people consider graph/object DB (like mongo) a superset of relational database functionally wise, so you can use mongo as a substitution in most cases, the main concerns left are: performance, security, DB adapters and stability. Have fun, its always good to try out new technologies. Personally, I think mongoDB is not mature enough, yet a promising replacement to relational DB. – Winfred Apr 15 '11 at 07:50
  • @Winfred, yes sorry you'r right it's one 2 many. – Luc Apr 15 '11 at 08:06

2 Answers2

2

In general mongodb will be good replacement of relational database for above task.

So some basics:
1.Once some one take a book you just need to copy book into the nested collection of user and user to the Book.
2.Once user has updated his profile you need aslo update information about user within Book.
3.Once book data was changed you also need update info about book within user.
4.If you trying to delete some book and current borrower exists you should say that book was borrowed by 'User' and not delete it.

I just suggest to add into your schema instead of current_borrower_email entire User object -> current_borrower: UserSchema.

So with such denormalized schema you will able easy show(within one request to mongodb):

  • the list of books currently borrowed by a user.
  • the current borrower of a given book
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
  • @andrew-orsich, ok thanks that confirm my though. Otherwise, what do you think of an additionnal table for many to many relationship (Winfred comments), I guess this could really help. – Luc Apr 15 '11 at 07:45
  • @Luc: About additional table in monodb -- no and again no. If you want additional table just use sql for your task. – Andrew Orsich Apr 15 '11 at 07:48
  • @andrew-orsich, ok, got it. Also if I use current_borrower: UserSchema, could there be a risk of loop because User also have books: [BookSchema] ? Also, I remember trying "property : Schema" but it seems it was not working (only properties : [Schema]) worked. any idea ? – Luc Apr 15 '11 at 08:09
  • @Luc: All will be okay. And i don't know how to do it in mongoose, i am .net guy ;). – Andrew Orsich Apr 15 '11 at 08:14
0

It is an old question but it came first in google so... It's not too complicated but it is too long to summarize. Read this: http://blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part-1

ozma
  • 1,633
  • 1
  • 20
  • 28