Nid*_*min 2 mongodb mongodb-php mongodb-query aggregation-framework php-mongodb
我写了下面的代码行来获取查找中的一些特定字段,例如
$pipeline = array(
array(
'$match' => $query
),
array(
'$lookup' => array(
'from' => 'studentTbl',
'localField' => '_id',
'foreignField' => 'activity_details.activityId',
'pipeline' => [
['$project' => [ '_id' => 1.0, 'activity_details' => 1.0] ],
],
'as' => 'studentsOfActivities'
)
),
....
....
);
return $this->db->activitiesTbl->aggregate($pipeline)->toArray();
Run Code Online (Sandbox Code Playgroud)
基本上 studentTbl 有许多字段和嵌入的文档。在上面的代码中,我首先使用外部和本地字段通过查找获取,然后确定哪些字段应该投影到管道内。
上面的代码不起作用...请帮忙!!!
您可以使用以下聚合
db.collection.aggregate([
{ "$match": $query },
{ "$lookup": {
"from": "studentTbl",
"let": { "activityId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$in": ["$$activityId", "$activity_details.activityId"] }}},
{ "$project": { "activity_details": 1 }}
],
"as": "studentsOfActivities"
}}
])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3716 次 |
| 最近记录: |