所以这是我的尝试,它不起作用,我不确定我需要做什么,解释这个的文档只是承认。
所以我希望这里有人知道如何做到这一点。
基本概念:我已将用户最新的个人资料图片ID存储在用户主要个人资料集合中
成员集合
{
"_id" : ObjectId("aaaat656a464"),
"email" : "russell@ipet.xyz",
"personal" : {
"name" : {
"firstname" : "Russell",
"lastname" : "Harrower"
},
"profile_id" : ObjectId("333a0e2b7acebe9b869b1b0a")
},
"kst" : ObjectId("111111111111g")
}
现在我想从members_media集合中获取profile_id,其中_id = profile_id
这是members_media集合
{
"_id" : ObjectId("333a0e2b7acebe9b869b1b0a"),
"data" : {
"url" : "https://scontent.xx.fbcdn.net/v/t1.0-1/11659354_1625058617782022_7822649569017662262_n.jpg?oh=6e2b74b33c1c4ea4cdc0094a1ae35e14&oe=59DF809A",
"type" : "facebook/image",
"date" : ISODate("2017-06-21T06:16:18.580Z"),
"profile_pic" : true
},
"uid" : ObjectId("111111111111g")
}
所以这是我的尝试
$check = $db->aggregate([
['$unwind'=> "$members_media"],
['$unwind' => ['path'=>"$members_media", 'includeArrayIndex'=>"arrayIndex"]],
['$lookup'=>
[
'from'=>"members_media",
'localField'=>"personal.profile_id",
'foreignField'=>"_id",
'as'=>"profilepic"
]
]
]);
但是,当我运行以下内容时,它不会获得成员集合
$db = static::db()->members;
/*$check = $db->findOne(['kst' => New MongoDB\BSON\ObjectId($_SESSION["ipet_user"]), 'personal'=>['$exists'=>true]],
['personal.profile_id' => 1]
);*/
$check = $db->aggregate([
['$lookup' =>
[
'from'=>"members_media",
'localField'=>"members.personal.profile_id",
'foreignField'=>"_id",
'as'=>"data"
]
],
['$match' =>
['members.kst' => New MongoDB\BSON\ObjectId($_SESSION["user"]), 'members.personal'=>['$exists'=>true]]
],
['$unwind' => ['path' => '$members_media.data', 'includeArrayIndex' => 'arrayIndex']],
['$project' =>
['members.profile_path'=>'$data.url']
]
]);
我的结果是
object(MongoDB\Driver\Cursor)#19 (9) { ["database"]=> string(4) "ipet" ["collection"]=> NULL ["query"]=> NULL ["command"]=> object(MongoDB\Driver\Command)#18 (1)
答案 0 :(得分:0)
我们可以这样做
$resultData = $db->collectionName->aggregate([
['$lookup' => [
'from' => 'members_media',
'localField' => 'personal.profile_id',
'foreignField' => '_id',
'as' => 'profileDetail',
]]]);
$myData = array();
foreach($resultData as $result) {
//var_dump($result);
array_push($myData, $result);
}
print_r($myData);