简体   繁体   English

MongoDB - 查询多对多关系如何工作?

[英]MongoDB - How does query for many-to-many relation work?

I am reading the book MongoDB in Action . 我正在阅读MongoDB in Action一书。 I have a question about Chapter 4: Document-oriented data . 我有一个关于Chapter 4: Document-oriented data的问题Chapter 4: Document-oriented data

On Page 58 , the book gives an example for many-to-many relations. Page 58 ,本书给出了多对多关系的一个例子。 It gives Product document and Category document. 它提供Product文档和Category文档。

Product 产品

product_1

product_2

Category 类别

category_1category_2


My question 我的问题

I understand the many-to-many association here. 我理解这里的多对多关联。 Basically, Product can have a key with array of Category _id, etc. So I am not trying to ask a question like this MongoDB Many-to-Many Association 基本上, Product可以有一个Category _id等数组的键。所以我不是想问一个类似MongoDB多对多关联的问题

My question is about Page 61 , where the book gives two example queries about querying the many-to-many relations. 我的问题是关于Page 61 ,其中本书提供了两个关于查询多对多关系的示例查询。 Here is the two queries: 这是两个查询:

两个问题

  1. What does => mean? =>是什么意思? I thought => only exists in Ruby driver usage. 我以为=>仅存在于Ruby驱动程序用法中。

  2. What is the category in category['_id'] ? 什么是categorycategory['_id'] Is it a collection? 这是一个集合吗?

  3. What is the product in product['category_ids'] ? 什么是productproduct['category_ids']

  4. How is the first query related to Gardening Tools category as described above the first query? 第一个查询如何与第一个查询相关的第一个查询与上面描述的Gardening Tools category相关?

The book doesn't explain these two queries in details. 本书没有详细解释这两个问题。

Can someone explain more about querying for many-to-many? 有人可以解释更多关于查询多对多的信息吗?

It does look like the author is using a driver's language other than the native MongoDB javascript mongo client. 看起来作者使用的是除本机MongoDB javascript mongo客户端之外的驱动程序语言。 Also it would make sense since the book description says the author maintains both the C and Ruby Mongo drivers. 这也是有意义的,因为书中描述说作者维护了C和Ruby Mongo驱动程序。

  1. Yes the => is a language driver specific notation. 是的=>是特定于语言驱动程序的表示法。 Not javascript. 不是javascript。 Seems to be just like saying: db.products.find({_id: aCategoryId}) . 似乎就像说: db.products.find({_id: aCategoryId}) Its actually a Ruby Hash notation . 它实际上是一个Ruby Hash表示法

  2. The category in that example looks to represent just a category document that you have already retrieved. 该示例中的category看起来只代表您已检索的类别文档。 In this case, it would have been the doc for the Gardening Category. 在这种情况下,它将是园艺类别的文档。 Its just saying "find all products where this category id is in the products category_ids array 它只是说“找到这个类别id在产品category_ids数组中的所有产品

  3. Similar to the previous question. 与前一个问题类似。 product is a document you have retrieved already. product是您已检索过的文档。 The query is saying "find any category doc where its id is IN this products array of category ids. 该查询说“查找任何类别的文档,其ID为此产品类别ID数组。

  4. category would be the Gardening Category if you had retrieved it with something like: var category = db.category.findOne({slug: "gardening-tools"}) 如果您使用以下内容检索它,则category将是园艺类别: var category = db.category.findOne({slug: "gardening-tools"})

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

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