本地字段和外部字段的查找管道内的项目不起作用
Project inside pipeline of lookup with local field and foriegn field not working
我写了下面的代码行,用于在查找中获取一些特定字段,例如
$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();
基本上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"
}}
])
你好,如果你们正在寻找这样的查询,这里是答案:
在 MongoDB.
中使用具有本地字段和外部字段内部聚合方法的项目
db.collection.aggregate([
{
$lookup: {
from: "users", // collection name in db
localField: "user_id",
foreignField: "_id",
as: "userdata",
},
},
{$match :{_id: {$in:losers} }},
{$project:
{
"answer":1,"_id":1,"win_status":1,"user_id":1,"user_id":1,
"userdata":{"name":1,"image":1,"email":1},
}
}
])
我写了下面的代码行,用于在查找中获取一些特定字段,例如
$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();
基本上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"
}}
])
你好,如果你们正在寻找这样的查询,这里是答案: 在 MongoDB.
中使用具有本地字段和外部字段内部聚合方法的项目db.collection.aggregate([
{
$lookup: {
from: "users", // collection name in db
localField: "user_id",
foreignField: "_id",
as: "userdata",
},
},
{$match :{_id: {$in:losers} }},
{$project:
{
"answer":1,"_id":1,"win_status":1,"user_id":1,"user_id":1,
"userdata":{"name":1,"image":1,"email":1},
}
}
])