1

我正在使用 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"
}

如何在 php 中使用 Mongodb 编写此 sql?

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

1 回答 1

1

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

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

另请参阅:很可能您也提出了这个问题(但是在答案中,如果您设计一种预先连接的文档结构,它会非常明智地描述): mongodb php - how to do "INNER JOIN"-like query

于 2013-06-06T11:47:14.410 回答