如何模型客户> order> ordertem>产品在NoSql数据库? [英] How would you model customer > order > ordertem > product in NoSql database?

查看:90
本文介绍了如何模型客户> order> ordertem>产品在NoSql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Node.JS,需要实现一个数据库。所有的Node的书似乎认为MongoDB是最好的解决方案,但我似乎不能得到我的头像NoSQL数据库像Mongo和Couch,我是一个MS SQL Server家伙!

I'm currently learning Node.JS and need to implement a database. All of the Node books seem to think MongoDB is the best solution but I can't seem to get my head around NoSql databases like Mongo and Couch, I'm an MS SQL Server guy!

所以,我知道你可以保持结构化数据作为记录(JSON),但我不知道你将如何建模一个典型的电子商务应用程序与以下(简化)表...

So, I understand that you can keep structured data as records (JSON) but I'm not sure how you'd model a typical ecommerce app with the following (simplified) tables...

customers (id, name, address)
orders (id, customerID, orderDate)
orderItems (id, orderID, productID)
products (id, title, description, image)

编写一个这样的查询(但更好地优化显然)....

So, normally I'd write a query like this (but better optimized obviously)....

SELECT Customers.name, Products.title 
FROM (orders INNER JOIN customers ON orders.customerID = customers.id)
INNER JOIN orderItems ON orderItems.orderID = orders.id 
INNER JOIN products ON orderItems.productID = products.id 

如果我可以看到一个示例如何在NoSQL数据库中工作,那么我可能开始获得它。

If I could see an example of how this would work in a NoSQL database then I might start to "get it".

或者,我只是更好地坚持使用MSSQL Server或MySql,这两者都与Node兼容?

Alternatively, am I just better off sticking with MSSQL Server or MySql, both of which are compatible with Node anyway?

推荐答案

在为MongoDB设计模式时,一个重要的考虑不是你的数据是什么,而是你将如何使用它。没有找出你将要做什么类型的读写操作(以及它们的表现如何),很难设计一个最优模式。

An important consideration when designing a schema for MongoDB is not what your data is, but how you will be using it. Without working out what type of reads and writes you will be doing (and how performant they will be) it can be difficult to design an "optimal" schema.

一些基本的准则,你可以考虑避免遇到问题。其中一个是避免设计文档,不断增长无限。这意味着您不应该将订单嵌入客户文档。另一个规则是,不感兴趣的东西本身(或不存在自己)可能更好地被嵌入。这表明orderItems不值得拥有自己的收藏,应该只是作为订单的属性(这是他们是,事实上)。

There are some basic guidelines that you can consider to avoid running into problems. One of them is avoid designing documents which keep growing unbounded. That means you should not embed orders into customer documents. Another rule is that things that aren't "of interest" on their own (or don't exist on their own) are probably better off being embedded. This suggests that orderItems do not deserve their own collection and should simply be treated as attributes of orders (which is what they are, in fact).

这个确切的练习

底线是你应该有三个集合:

Bottom line is that you should have three collections:

产品

客户

订单

Products
Customers
Orders

订单将引用客户(可选择取消客户收集的某些信息的标准化)它们将引用产品(在它们包含的orderItem数组中)。

Orders will reference customers (optionally denormalizing some information from customer collection) and they will reference products (in the array of orderItems they will contain).

更多集合,所有这些集合中的确切字段取决于您的具体用例,但我可以没有看到一个可行的情况,拥有比这三个更少的集合。

Further collections, and exact fields in all these collections depend on your specific use case, but I can't see a feasible scenario to have fewer collections than these three.

这篇关于如何模型客户> order> ordertem>产品在NoSql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆