Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Last active July 8, 2016 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpablocs/ecd62fd646ef52c95e5f5bf3efa23a16 to your computer and use it in GitHub Desktop.
Save juanpablocs/ecd62fd646ef52c95e5f5bf3efa23a16 to your computer and use it in GitHub Desktop.
mongo relational join php

##Mongo relation## test php mongo relational songs and users

init variables

<?php
$connect  = new \MongoClient($this->settings['mongo_connection']);
$database = $connect->mydb;
$col_mp3  = $database->mp3;
$col_user = $database->user;

Insert mp3 with user-id

<?php
$test1 $col_user->insert(['name'=>'Juan','user_id'=>10]);
var_dump($test1);
$user = $col_user->find(['user_id'=>10])->getNext();
$test2 = $col_mp3->insert([ 'name'=>'song1','mp3'=>'song1.mp3','user_id'=>$user['_id'] ]);
var_dump($test2);

find mp3 and join user

<?php
$mp3s = $col_mp3->find([]);
$r 	  = [];
foreach ($mp3s as $mp3) {
	$mp3['user'] = $col_user->find([ '_id'=> $mp3['user_id'] ])->getNext();
	$r[] = $mp3;
}
print_r($r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment