简体   繁体   中英

NoSQL db schema design

I'm trying to find a way to create the db schema. Most operations to the database will be Read.

Say I'm selling books on the app so the schema might look like this

{
 { title : "Adventures of Huckleberry Finn"
  author : ["Mark Twain", "Thomas Becker", "Colin Barling"],
  pageCount : 366,
  genre: ["satire"] ,
  release: "1884",
 },
 { title : "The Great Gatsby"
  author : ["F.Scott Fitzgerald"],
  pageCount : 443,
  genre: ["Novel, "Historical drama"] ,
  release: "1924"
 },
 { title : "This Side of Paradise"
  author : ["F.Scott Fitzgerald"],
  pageCount : 233,
  genre: ["Novel] ,
  release: "1920"
 }
}

So most operations would be something like

1) Grab all books by "F.Scott Fitzgerald"
2) Grab books under genre "Novel"
3) Grab all book with page count less than 400
4) Grab books with page count more than 100 no later than 1930

Should I create separate collections just for authors and genre and then reference them like in a relational database or embed them like above? Because it seems like if I embed them, to store data in the db I have to manually type in an author name, I could misspell F.Scott Fitzgerald in a document and I wouldn't get back the result.

First of all i would say a nice DB choice.

As far as mongo is concerned the schema should be defined such that it serves your access patterns best. While designing schema we also must observe that mongo doesn't support joins and transactions like SQL. So considering all these and other attributes i would suggest that your choice of schema is best as it serves your access patterns. Usually whenever we pull any book detail, we need all information like author, pages, genre, year, price etc. It is just like object oriented programming where a class must have all its properties and all non- class properties should be kept in other class. Taking author in separate collection will just add an extra collection and then you need to take care of joins and transactions by your code. Considering your concern about manually typing the author name, i don't get actually. Let's say user want to see books by author "xyz" so he clicks on author name "xyz" (like some tag) and you can fetch a query to bring all books having that selected name as one of the author. If user manually types user name then also it is just finding the document by entered string. I don't see anything manual here. Just adding on, a price key shall also fit in to every document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM