user2458592
user2458592

Reputation: 11

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

I'm using the Mongo PHP extension and I want to join 2 collections in php. I searched around but people usually say "you can use map-reduce", but I can not do that in php.

My data looks like this:

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"
}

How can I write this sql with Mongodb in php?

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

Upvotes: 1

Views: 4378

Answers (1)

attish
attish

Reputation: 3150

You have to implement some algorithm inside your application: For example implement this :
http://en.wikipedia.org/wiki/Nested_loop_join http://en.wikipedia.org/wiki/Sort-merge_join

So basically there is no easy solution instead of the one described here : MongoDB-PHP: JOIN-like query

And see also : Most probably you put on this question also (But in the answers it described that much wise if you design the document structure kind of pre joined way): mongodb php - how to do "INNER JOIN"-like query

Upvotes: 1

Related Questions