【问题标题】:Aggregation in find query is not working while using aggregate method on collection do在集合上使用聚合方法时,查找查询中的聚合不起作用
【发布时间】:2014-04-28 10:12:37
【问题描述】:

工作并返回我一条记录

db.gfk_products.aggregate([
    {$match : {product_id : '50910288'}},
    {$project : {
        features: "$features.key"
    }}
])

不返回任何内容

db.gfk_products.find({

    aggregate : [
        {$match : {product_id : '50910288'}},
        {$project : {
            features: "$features.key"
        }}
    ]
})

不知道为什么!第二个查询是由 cakephp 的 mongodb 适配器生成的。

编辑

Cakephp 代码

$cond['conditions'] = array(
    'aggregate' => array(
        array(
            '$match' => array(
                'product_id' => $itemID
            ),
        ),
        array(
            '$project' => array(
                'features' => '$features.key'
            ),
        ),
    ),
);
$result = $this->Product->find('all',$cond);

【问题讨论】:

  • 我不认为你可以从 find 内部进行聚合。您能否指出您在 MongoDB 文档中看到此类查询的文档?
  • 您实际上是如何在 CakePHP 中调用它的?实际上有一些关于这个here 的注释。如果不清楚,也许您可​​以将您正在使用的代码添加到您的问题中。
  • @NeilLunn 添加了相应的 cakephp 代码。我已经按照相同的结构浏览了您共享的链接。
  • 根据我在上一条评论中给出的链接,如果你只是减少到$cond = array( 'aggregate' 等,这仍然会失败吗?
  • 是的,它没有。那么我将不得不将 find 调用更改为 $this->find->('all', array('conditions' => $cond)) ,这是完全相同的......请参阅此处stackoverflow.com/questions/20897382/…

标签: mongodb cakephp mongodb-php


【解决方案1】:

Ankit,

MongoDB 中没有通过 find 查询执行聚合管道的工具。您可以在 MongoDB/PHP 驱动程序 - http://www.php.net/manual/en/mongocollection.aggregate.php 中找到有关专门用于聚合的方法的更多详细信息。

【讨论】:

  • 这就是我最终要做的。但不知何故,cakephp 驱动程序以错误的方式生成它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 2013-12-06
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 2019-06-05
相关资源
最近更新 更多