我如何在Php程序语言中加入Mongodb的2个集合


How can I join 2 collections with Mongodb in Php program language?

我正在使用Mongo PHP扩展,我想在PHP中加入2个集合。我四处搜索,但人们通常说"你可以使用map-reduce",但我不能在php中这样做。

我的数据是这样的:

users
{
  "_id": "4ca30369fd0e910ecc000006",
  "login": "user11",
  "pass": "example_pass",
  "date": "2010-09-29"
},
{
  "_id": "4ca30373fd0e910ecc000007",
  "login": "user22",
  "pass": "example_pass",
  "date": "2010-09-29"
}
news
{
  "_id": "4ca305c2fd0e910ecc000003",
  "name": "news 333",
  "content": "news content 3333",
  "user_id": "4ca30373fd0e910ecc000007",
  "date": "2010-09-29"
},
{
  "_id": "4ca305c2fd0e910ecc00000b",
  "name": "news 222",
  "content": "news content 2222",
  "user_id": "4ca30373fd0e910ecc000007",
  "date": "2010-09-29"
},
{
  "_id": "4ca305b5fd0e910ecc00000a",
  "name": "news 111",
  "content": "news content",
  "user_id": "4ca30369fd0e910ecc000006",
  "date": "2010-09-29"
}

我怎么能写这个sql与Mongodb在php?

SELECT n.*, u.* 
FROM news AS n 
INNER JOIN users AS u ON n.user_id = u.id

你必须在你的应用程序中实现一些算法:例如:
http://en.wikipedia.org/wiki/Nested_loop_joinhttp://en.wikipedia.org/wiki/Sort-merge_join

所以基本上没有简单的解决方案,而不是这里描述的:MongoDB-PHP: JOIN-like query

参见:最有可能你也提出了这个问题(但在答案中,如果你以一种预先连接的方式设计文档结构,它会描述得更明智):mongodb php -如何做"INNER JOIN"类查询