【问题标题】:Why $cursor return 45 limits when I limit the number of returned result to 40当我将返回结果的数量限制为 40 时,为什么 $cursor 返回 45 限制
【发布时间】:2012-08-22 05:58:48
【问题描述】:
    $startplushowmany=$startfrom+$howmany; //$startplush is 40
    $cursor=$cursor->limit($startplushowmany);  

    $numberReturned=$cursor->count();   //$numberReturned is 45

我不能给你看 $cursor 的内容,因为 $cursor 没有实现 _toString。请告诉我该怎么做。

现在 $cursor 中的查询由以下命令定义: $cursor = $collection->find($rangeQuery, $field);

$rangeQuery 在哪里

(string:592) Array
(
    [LongitudeLatitude] => Array
        (
            [$within] => Array
                (
                    [$center] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 106.772835
                                    [1] => -6.186753
                                )

                            [1] => 0.044983732050783
                        )

                )

        )

    [indexContents] => bas
    [Prominent] => Array
        (
            [$gte] => 15
        )

)

$字段是

(string:39) Array
(
    [LongitudeLatitude] => 1
)

【问题讨论】:

    标签: php json mongodb


    【解决方案1】:

    您需要使用布尔标志来考虑计数限制,否则它会给出实际的 计数

    $numberReturned=$cursor->count(true);

    引用PHP doc的例子:

    <?php
    
    $collection->insert(array('x'=>1));
    $collection->insert(array('x'=>2));
    $collection->insert(array('x'=>3));
    
    $cursor = $collection->find();
    
    var_dump($cursor->count());
    var_dump($cursor->count(true));
    
    $cursor->limit(2);
    
    var_dump($cursor->count());
    var_dump($cursor->count(true));
    
    ?>
    

    将输出:

    int(3)
    int(3)
    int(3)
    int(2)
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2016-03-29
      • 2012-12-14
      • 2021-10-07
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多