应该可以在没有Map Reduce操作的情况下检索所需的信息。
您可以首先查询匹配{'enabled':1}的文档的“Products”集合,然后从该查询中获取$ SHOP_ID的列表(我想像的是与“Shops”中的_id值相对应的列表)集合),将它们放在一个数组中,然后在“Shops”集合中执行$ in查询,并在“name”上结合查询。
例如,给出的两个集合:
> db.products.find()
{ "_id" : 1, "type" : "first", "enabled" : 1, "shop" : 3 }
{ "_id" : 2, "type" : "second", "enabled" : 0, "shop" : 4 }
{ "_id" : 3, "type" : "second", "enabled" : 1, "shop" : 5 }
> db.shops.find()
{ "_id" : 3, "name" : "L" }
{ "_id" : 4, "name" : "L" }
{ "_id" : 5, "name" : "M" }
>
首先找到所有匹配的文件{ “已启用”:1}
> db.products.find({"enabled" : 1})
{ "_id" : 1, "type" : "first", "enabled" : 1, "shop" : 3 }
{ "_id" : 3, "type" : "second", "enabled" : 1, "shop" : 5 }
从上面的查询,生成的列表_ids:
> var c = db.products.find({"enabled" : 1})
> shop_ids = []
[ ]
> c.forEach(function(doc){shop_ids.push(doc.shop)})
> shop_ids
[ 3, 5 ]
最后,在shop_ids数组tha中查询具有_id值的文档的商店集合t也匹配{名称:“L”}。
> db.shops.find({_id:{$in:shop_ids}, name:"L"})
{ "_id" : 3, "name" : "L" }
>
以前有人问过关于做与Mongo等同的加入操作的类似问题。这个问题提供了一些链接可以为您提供额外的指导:
How to join MongoDB collections in Python?
如果你想用地图试验降低,这里是从谁使用增量的Map Reduce操作结合用户在博客帖子的链接来自两个集合的值。
http://tebros.com/2011/07/using-mongodb-mapreduce-to-join-2-collections/
希望以上将允许您从您的收藏中检索所需的信息。
您是否需要频繁地将这些集合加入到一起?如果是这样,你可能要重新考虑使用MongoDb或重新考虑一下你的模式。 – 2012-04-18 20:55:26
你好,你有没有这个运气? Marc的回答看起来相当不错,我认为应该得到某种反馈。 – halfer 2015-10-11 21:56:18
实际上,我们搬到了一个sql友好的数据库。蒙戈对其他业务好 – 2015-10-12 15:14:48