简体   繁体   English

MongoDB多对多关系逻辑

[英]MongoDB many-to-many relationship logic

I'm trying to design a schema for Products , Suppliers and Manufacturers : 我正在尝试为产品供应商制造 设计模式:

  1. A product can have many suppliers but only 1 manufacturer. 一个产品可以有许多供应商,但只有1个制造商。
  2. A supplier can have many products and many manufacturers. 一个供应商可以拥有许多产品和许多制造商。
  3. A manufacturer can have many suppliers and many products. 制造商可以有许多供应商和许多产品。

I've reviewed this page where 10gen indicates "To avoid mutable, growing arrays, store the publisher reference inside the book document". 我查看了页面,其中10gen指示“为避免可变的,增长的数组,请将出版商的参考文献存储在书本文档中”。 In my example I consider the product-->manufacturer relationship to be equivalent to that of book-->publisher . 在我的示例中,我认为产品->制造商关系与书籍->发布者关系相同。 I would therefore do this: 因此,我将这样做:

{
   _id: "widgets",
   manufacturer_name: "Widgets Inc.",
   founded: 1980,
   location: "CA"
}

{
   _id: 123456789,
   product_name: "Steel Widget",
   color: "Steel",
   manufacturer_id: "widgets"
}

{
   _id: 223456789,
   product_name: "White Widget",
   color: "White",
   manufacturer_id: "widgets"
}    

What is the best way to handle the SUPPLIER (with relationships to many products and many manufacturers) such that I "avoid mutable, growing arrays"?? 处理供应商(与许多产品和许多制造商有关系)以便避免“可变的,不断增长的阵列”的最佳方式是什么?

Note: This is one way to model it. 注意:这是建模的一种方法。 Data modeling has a lot to do with the use cases and the according questions you want to ask. 数据建模与用例和您要提出的相应问题有很大关系。 Your use case might need a different model. 您的用例可能需要其他模型。

I'd probably model it like this 我可能会像这样建模

manufacturer 生产厂家

{
  _id:"ACME",
  name: "ACME Corporation"
  …
}

products 制品

{
  _id:ObjectId(...),
  manufacturer: "ACME",
  name: "SuperFoo",
  description: "Without SuperFoo, you can't bar or baz!",
  …
}

Now comes the problem. 现在出现了问题。 Since potentially, if we embed all the products in a supplier document or vice versa, we could break the 16MB size limit easily, I'd use a different approach: 既然有可能,如果我们将所有产品都嵌入到供应商文档中,反之亦然,则可以轻松突破16MB的大小限制,因此我将使用另一种方法:

Supplier: 供应商:

{
  _id:ObjectId(...),
  "name": "FooMart",
  "location: { city:"Cologne",state:"MN",country:"US",geo:[44.770833,-93.783056]}
}

productSuppliers: productSuppliers:

{
  _id:ObjectId(...),
  product:ObjectId(...),
  supplier:ObjectId(...)
}

This way, each product can have gazillions of suppliers. 这样,每个产品可以拥有数以百万计的供应商。 Now here come the drawbacks. 现在出现了弊端。

For finding a supplier for a certain product, you have to do a two step query. 为了找到某种产品的供应商,您必须执行两步查询。 First, you have to find all supplier IDs for a given product: 首先,您必须找到给定产品的所有供应商ID:

db.productSuppliers.find({product:<some ObjectId>},{_id:0,supplier:1})

Now, let's say we want to find all suppliers of SuperFoo near Cologne,MN at a max distance of 10 miles: 现在,假设我们要查找最大距离为10英里的明尼苏达州科隆附近的SuperFoo的所有供应商:

db.suppliers.find({
  _id:{$in:[<ObjectIds of suppliers from the first query>]},
  "location.geo": { $near :
    {
      $geometry: { type: "Point",  coordinates: [ 44.770833, -93.783056] },
      $maxDistance: 16093
    }
  }

})

You need to do quite some smart indexing to make these queries efficient. 您需要做一些智能索引才能使这些查询高效。 Which indices you need depends on your use case. 您需要哪些索引取决于您的用例。 The problem with indices is that they are only efficient when kept in RAM. 索引的问题在于它们仅在保留在RAM中时才有效。 So you really should be careful when creating your indices. 因此,在创建索引时,您确实应该小心。

Again: The way you model data heavily depends on your use cases. 同样:数据建模的方式很大程度上取决于用例。 In case you only have a few products per manufacturer or only a few suppliers per product or if each supplier only supplies products by a certain manufacturer, the model may look quite different. 如果每个制造商只有几个产品,或者每个产品只有几个供应商,或者每个供应商仅提供某个制造商的产品,则该模型看起来可能会大不相同。

Please have a close look at MongoDB's data modeling documentation . 请仔细查看MongoDB的数据建模文档 Most problems with MongoDB stem from wrong data modeling for the respective use case. MongoDB的大多数问题源于相应用例的错误数据建模。

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

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