[英]PHP mongoDB distinct selection
This is my first time posting here in stackoverflow. 这是我第一次在这里发布stackoverflow。 I am hoping someone can help me with my issue with mongoDB PHP.
我希望有人可以帮助我解决mongoDB PHP的问题。 I am more of a PHP mySql guy but we suddenly needed to switch to mongoDB.
我更像是一个PHP mySql家伙,但我们突然需要切换到mongoDB。 Below is the problem situation in the most explanation that I can give.
以下是我能给出的最多解释中的问题情况。
Imagine in mysql, I have a table named "photos" with 3 fields (img_id, owner_id, and file_path). 想象在mysql中,我有一个名为“ photos”的表,其中包含3个字段(img_id,owner_id和file_path)。 And with record below:
并记录如下:
Record #1: 1---1---image01.jpg 记录#1: 1 --- 1 --- image01.jpg
Record #2: 2---2---image02.jpg 记录#2: 2 --- 2 --- image02.jpg
Record #3: 3---2---image03.jpg 记录#3: 3 --- 2 --- image03.jpg
Then I have another table named "users" with 2 fields (user_id, fullname) with records below: 然后,我有另一个名为“用户”的表,其中包含以下记录的2个字段(user_id,全名):
Record #1: 1---Jhon Doe 记录#1: 1-庄道
Record #2: 2---Mark Kane 记录2: 2-马克·凯恩(Mark Kane)
All I need is to display results similar to below: 我需要显示的结果类似于以下内容:
1.) John Doe has 1 image and the image id is 1 . 1.) John Doe有1张图片,其图片ID为1 。 - image01.jpg
-image01.jpg
2.) Mark Kane has 2 images and the image ids are 2 and 3 . 2.) 马克·凯恩(Mark Kane)有2张图片,图片ID为2和3 。 - image01.jpg, image02.jpg
-image01.jpg,image02.jpg
Please let me know how this should be done in mongoDB PHP. 请让我知道如何在mongoDB PHP中完成此操作。 Thanks in advance.
提前致谢。
$m = new Mongo();
$collection = $m->users;
$collection1 = $m->photos;
$cursor = $collection->find();
$cursor1 = $collection1->find();
$count = 0;
$result = array();
$path = array();
$str = "";
$str1 = "";
foreach($cursor as $obj){
foreach($cursor1 as $obj1){
if($obj['user_id'] == $obj1['owner_id']){
$result[$count] = $obj1['img_id'];
$path[$count] = $obj1['file_path'];
$count++;
}
}
if($count > 1){
for($i = 0; $i < $count; $i++){
$str .= $result[$count]." and ";
$str1 .= $path[$count]." , ";
}
}
else{
$str = $result[0];
$str1 = $path[0];
}
print "$obj['fullname'] has $count image and the image id is $str - $str1 .";
}
Hope the above code block gives you the desired result, the code is not tested, so some compiler or syntactical errors may be there, but I hope you can solve them. 希望以上代码块能为您提供所需的结果,并且代码未经测试,因此可能存在一些编译器或语法错误,但希望您可以解决它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.