【发布时间】:2012-05-28 21:17:02
【问题描述】:
我正在阅读MongoDB in Action,在谈到查询文档中的多对多关系时,我很难理解他是如何编写示例查询的(使用 Ruby 驱动程序)。
查询查找特定类别中的所有产品,其中有 products 和 category 集合。作者说“查询园艺工具类的所有产品,代码很简单:
db.products.find({category_ids => category['id']})
产品文档是这样的:
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"],
}
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!",
}
有人可以向我解释一下吗?我还是不明白他是怎么写那个查询的:(
谢谢大家。
【问题讨论】:
标签: mongodb mongodb-query mongodb-ruby