开发者

Can I use NoSQL instead of a Relational Database?

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 t开发者_如何学Pythono 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 ?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜