简体   繁体   English

NoSQL数据库架构设计

[英]NoSQL db schema design

I'm trying to find a way to create the db schema. 我正在尝试找到一种方法来创建数据库模式。 Most operations to the database will be Read. 对数据库的大多数操作都是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. 因为看起来我嵌入它们,为了在db中存储数据我必须手动输入作者名称,我可能会错误地将F.Scott Fitzgerald放入文档中,我不会得到结果。

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. 就mongo而言,应该定义模式,以便最好地为您的访问模式提供服务。 While designing schema we also must observe that mongo doesn't support joins and transactions like SQL. 在设计模式时,我们还必须注意到mongo不支持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. 假设用户希望通过作者“xyz”查看书籍,以便他点击作者姓名“xyz”(如某些标签),您可以获取查询以将所有具有所选名称的书籍作为作者之一。 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. 只需添加,价格键也适用于每个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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