$ match查询不适用于mongodb中的查找数组

时间:2018-12-26 11:18:33

标签: mongodb mongodb-query aggregation-framework mongodb-php php-mongodb

我在mongodb数据库中有两个表

 activityCountTbl contains data like

        {
             "_id": ObjectId("5c234f7e3250041280000ca3"),
             "activityId": ObjectId("5c0e27ee590a06bf08003c33"),
             "weekgroupId": ObjectId("5bfbddbcbb2c5645f8001495"),
             "squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
             "attendingCount": NumberInt(6),
             ....
        }  

        {
             "_id": ObjectId("5c234f7e3250041280000ca3"),
             "activityId": ObjectId("5c0e27ee590a06bf08003c33"),
             "weekgroupId": ObjectId("5bfbddbcbb2c5645f8001496"),
             "squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
             "attendingCount": NumberInt(6),
             ....
        } 
   squadronTbl contains data like

         {
           "_id": ObjectId("5c19ccb7590a060691000554"),
           "squadronCode": "336",
           "squadronName": "336TRS",
         }

         {
           "_id": ObjectId("5c19ccb7590a060691000556"),
           "squadronCode": "337",
           "squadronName": "337TRS",
         }

我将一个星期组的特定活动的计数详细信息存储在activityCountTbl中。我正在使用activityCountTbl在squadronTbl上执行查找 用于获取特定星期组的中队详细信息。以下代码无法正常工作。

当我注释/删除$ query代码时,它将获取所有分组的所有中队。

        $query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"];   

        $pipeline = array(
            [
              '$match' => $query
            ],  
            [
              '$lookup' => [
                'from' => 'activityCountTbl',
                'localField' => '_id',
                'foreignField' => 'squadronId',
                'as' => 'ActivityArray'
              ]
            ],
            ['$project' => [
                '_id' => 1.0,
                'squadronName' => 1.0,
                'ActivityArray' => 1.0
            ]],
        );

        return $this->db->squadronTbl->aggregate($pipeline)->toArray();

请帮助!!!

1 个答案:

答案 0 :(得分:0)

$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"]

$pipeline = array(
    [
      '$match' => []
    ],  
    [
      '$lookup' => [
        'from' => 'activityCountTbl',
        'localField' => '_id',
        'foreignField' => 'squadronId',
        'as' => 'ActivityArray'
      ]
    ],
    [
      '$match' => $query 
    ],
    ['$project' => [
        '_id' => 1.0,
        'squadronName' => 1.0,
        'ActivityArray' => 1.0
    ]],
);

类似的东西