MongoDb $facet聚合查询问题 - 根据域 ID 在单独的限制中获取数据

Issue with MongoDb $facet Aggregation Query - Getting Data in Separate Limits Based on Domain IDs

提问人:Nitish Roy 提问时间:9/21/2023 更新时间:9/21/2023 访问量:15

问:

我在 PHP 应用程序中遇到了聚合查询问题。此查询的目的是从 MongoDB 集合中检索数据,同时根据域 ID 应用不同的限制。但是,查询不会根据每个域 ID 的单独限制提供预期结果。

下面提供的代码片段表示相关的聚合查询:

$domainLimits = array(
    1 => 5, // email_domain_id 1 limit
    2 => 4, // email_domain_id 2 limit
);

$opn = array(
    array('$match' => $match_array2),
    array('$project' => array(
        'contact_email_id' => '$contact_email_id',
        'publisher_id' => '$publisher_id',
        'contact_id' => '$contact_id',
        'email_domain_id' => '$email_domain_id',
        'cmp_value' => array(
            '$cmp' => array(
                '$usage_limit',
                '$usedlimit.' . date('l', $unsusbcribe[0]->schedule_date)
            )
        ),
    )),
    array('$match' => array(
        'cmp_value' => array('$gt' => 0)
    )),
    array('$group' => array(
        '_id' => '$contact_email_id',
        'object_id' => (array('$last' => '$_id')),
        'publisher_id' => array('$last' => '$publisher_id'),
        'contact_id' => array('$last' => '$contact_id'),
        'email_domain_id' => array('$last' => '$email_domain_id')
    )),
    array('$sort' => array('_id' => -1)),

    array('$group' => array(
        '_id' => '$email_domain_id',
        'docs' => array('$push' => '$$ROOT')
    )),
    array('$facet' => array(
        'customLimits' => array(
            array('$unwind' => '$docs'),
            array('$lookup' => array(
                'from' => 'email_domain_limits', // Replace with your collection name for domain-specific limits
                'localField' => '_id',
                'foreignField' => 'email_domain_id',
                'as' => 'limitData'
            )),
            array('$unwind' => '$limitData'),
            array('$replaceRoot' => array('newRoot' => array(
                '$mergeObjects' => array('$limitData', '$$ROOT')
            ))),
            array('$project' => array(
                'docs' => array(
                    '$cond' => array(
                        array('$eq' => ['$email_domain_id', '$_id']),
                        '$docs',
                        []
                    )
                )
            )),
            array('$unwind' => '$docs'),
            array('$group' => array(
                '_id' => '$_id',
                'docs' => array('$push' => '$docs')
            )),
            array('$project' => array(
                'docs' => array(
                    '$slice' => ['$docs', 0, array('$arrayElemAt' => array([$domainLimits, '$_id'], 0))]
                )
            )),
        ),
        'result' => array(
            array('$skip' => (int)$unsusbcribe[$i]->skip),
            array('$limit' => 10), // Default limit if no specific limit defined
        ),
    ))
);

$details = $this->mongo_db4->aggregate('opend_contacts_profile', $opn);
echo "<pre>";
print_r($details);
exit;

问题在于聚合逻辑,其中数据应根据域 ID 划分为单独的限制。但是,当前的实现似乎没有实现这一点,并且没有按域 ID 正确分割数据。

我需要帮助来排查和解决此问题。预期结果是根据指定的域 ID 限制在单独的限制中接收数据。对代码的任何指导或修改将不胜感激。

php mongodb mongodb-query 聚合框架 方面

评论


答: 暂无答案