简体   繁体   English

查询MongoDB中的多对多关系

[英]Query a Many-to-Many relation in MongoDB

I am reading MongoDB in Action and when talking about querying many-to-many relationships in a Document, I'm having difficulty understanding how he wrote his example query (using the Ruby driver). 我正在阅读《 MongoDB操作》 ,谈论查询文档中的多对多关系时,我很难理解他如何编写示例查询(使用Ruby驱动程序)。

The query is finding all products in a specific category, where there is a products and category collection. 该查询正在查找特定类别中的所有产品,其中有一个productscategory集合。 The author says "To query for all products in the Gardening Tool category, the code is simple: 作者说:“要查询“园艺工具”类别中的所有产品,代码很简单:

db.products.find({category_ids => category['id']})

A PRODUCT doc is like this: PRODUCT文档是这样的:

doc =
{ _id: new ObjectId("4c4b1476238d3b4dd5003981"),
  slug: "wheel-barrow-9092",
  sku: "9092",
  name: "Extra Large Wheel Barrow",
  description: "Heavy duty wheel barrow...",

  details: {
    weight: 47,
    weight_units: "lbs",
    model_num: 4039283402,
    manufacturer: "Acme",
    color: "Green"
  },

  category_ids: [new ObjectId("6a5b1476238d3b4dd5000048"),
                    new ObjectId("6a5b1476238d3b4dd5000049")],

  main_cat_id: new ObjectId("6a5b1476238d3b4dd5000048"),

  tags: ["tools", "gardening", "soil"],

}

And a CATEGORY doc is like this: CATEGORY文档如下所示:

doc =
{  _id: new ObjectId("6a5b1476238d3b4dd5000048"),
   slug: "gardening-tools",
   ancestors: [{ name: "Home",
                 _id: new ObjectId("8b87fb1476238d3b4dd500003"),
                 slug: "home"
                },

                { name: "Outdoors",
                 _id:  new ObjectId("9a9fb1476238d3b4dd5000001"),
                 slug: "outdoors"
               }
   ],

   parent_id: new ObjectId("9a9fb1476238d3b4dd5000001"),

   name: "Gardening Tools",
   description: "Gardening gadgets galore!",
}

Can someone please explain it a little more to me? 有人可以再给我解释一下吗? I still can't understand how he wrote that query :( 我仍然不明白他是怎么写这个查询的:(

Thanks all. 谢谢大家

The query is searching the products collection for all products with a value of category['id'] in the field category_ids 该查询正在搜索产品集合,以在字段category_ids查找值为category['id']所有产品

When you search a field that contains an array for a specific value, MongoDB automatically enumerates each value in that array searching for matches. 当您在包含数组的字段中搜索特定值时,MongoDB会自动枚举该数组中的每个值以查找匹配项。

To construct the query, you must first notice that the category collection defines your category hierarchy, and that each category has a unique ID (stored, as is usual in MongoDB, in the _id field) 要构造查询,您必须首先注意到category集合定义了您的类别层次结构,并且每个类别都有一个唯一的ID(通常在MongoDB中存储在_id字段中)

You must also notice that the product collection has a field that stores a list of category ids, category_ids , that reference the unique ids of the category collection. 还必须注意到, product集合了存储类别ID,列表的场category_ids ,引用了独特的ID category集合。

Therefore, to find all products in a particular category, you search the category_ids field of the product collection for the unique ID of the category you're interested in, which you get from the category collection. 因此,要查找特定类别中的所有产品,请在product集合的category_ids字段中搜索您感兴趣的类别的唯一ID,该ID是从category集合中获得的。

If I were to write a query for the Mongo javascript based shell interpreter, mongo that find products in the Gardening Tools category, I would do the following: 如果我要为基于Mongo javascript的Shell解释器( mongo编写查询,以在Gardening Tools类别中找到产品,则应执行以下操作:

  1. Look up the ID of the Gardening Tools category (which, as noted before, is stored in the _id field of the category collection) 查找“园艺工具”类别的ID(如前所述,存储在category集合的_id字段中)
    • In this case, the value in your example is ObjectId("6a5b1476238d3b4dd5000048") 在这种情况下,您的示例中的值为ObjectId("6a5b1476238d3b4dd5000048")
  2. Insert the value into a query that searches through the category_ids field of the product collection 将值插入查询中,以搜索product集合的category_ids字段
    • This is the query that you give in your question, which for the mongo shell I would write as: db.products.find({category_ids : new ObjectId("6a5b1476238d3b4dd5000048")}) 这是您在问题中提供的查询,对于mongo shell,我将其写为: db.products.find({category_ids : new ObjectId("6a5b1476238d3b4dd5000048")})

I hope that's clearer than the original explanation! 我希望这比原来的解释更清楚!

(As an aside: I'm not quite sure what language your query is written in, is it perhaps PHP? In any case, javascript seems to be the language of choice for examples in the MongoDB docs because the MongoDB server installs the mongo command line interpreter alongside the server itself, so everyone has access to it) (顺便说一句:我不太确定查询用哪种语言编写,也许是PHP?无论如何,JavaScript似乎是MongoDB文档中示例的首选语言,因为MongoDB服务器安装了mongo命令。服务器本身旁边的行解释器,因此每个人都可以访问它)

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

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