如何模型客户> order> ordertem>产品在NoSql数据库?

编程入门 行业动态 更新时间:2024-10-28 07:34:15
本文介绍了如何模型客户> 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数据库?

本文发布于:2023-11-06 16:23:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564204.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   客户   数据库   产品   NoSql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!