1

I have a mongo collection called "companies" which looks like this :

{
    "_id" : ObjectId("..."),
    "name" : "company_1",
    "active" : false,
    "projects" : [
        {
            "_id" : ObjectId("..."),
            "name" : "Prj_1",
            ...
        },
        {
            "_id" : ObjectId("..."),
            "name" : "Prj_2" ,
            ...
        }
    ],
    "rating" : 0,
    ...
}
...

I am using Kohana 3.2 as my framework and MangoDb library.

I want ot write a function to retrieve all the projects and list them for the user (Retrieving a Subset of Fields).

This is my company model:

<?php

class Model_Company extends Mango {

    protected $_db = 'default';
    protected $_collection = 'companies';
    protected $_fields = array(
        'name' => array(
            'type' => 'string',
            'required' => TRUE,
        ),
        'active' => array(
            'type' => 'boolean',
        ),
        'rating' => array(
            'type' => 'float',
        ),

        'projects' => array(
            'type' => 'has_many',
        ),
        ...
        ...
    );

    public function data_list($from = 0, $limit = 10, $sort = NULL)
    {
        return Mango::factory('company')->load(array( 'limit' => $limit, 'sort' => $sort, 'skip' => $from ));
    }

}

This is the embedded project model:

<?php

class Model_Project extends Mango {

    protected $_embedded = TRUE;
    protected $_db = 'default';
    //protected $_collection = 'projects';
    protected $_fields = array(
        '_id' => array(
            'type' => 'MongoId'
        ),
        'name' => array(
            'type' => 'string',
            'required' => TRUE,
        ),
        ...
        ...

    );

    public function data_list($from = 0, $limit = 10, $sort = NULL)
    {
        return Mango::factory('company')->load(array(
            'limit' => $limit, 
            'sort' => $sort, 
            'skip' => $from ,
            'fields'=>array('projects' => TRUE)
         ));
    }

}

When I try to run the line below:

$projects_list = Mango::factory('project')->data_list($from, $this->list_items_per_page)->as_array();

I get an error message listed below:

MongoCursorException [ 10105 ]: bad skip value in query

MODPATH\mongo\classes\mango\iterator.php [ 108 ]

103     /**
104      * Iterator: rewind
105      */
106     public function rewind()
107     {
108         $this->_cursor->rewind();
109     }
110 
111     /**
112      * Iterator: valid
113      */

What is the cause of this error and how can I solve this issue?

Braiam
  • 1
  • 11
  • 47
  • 78
YAAK
  • 328
  • 1
  • 14
  • I think it's because your variable **$from** is a negative value. Can you validate if it's the case ? – Devrun Oct 08 '12 at 19:47
  • @Devrun No, it's not negative. as I mentioned in one of my comments here, the error arises just when I try to call as_array() function on the results. – YAAK Oct 09 '12 at 15:16

2 Answers2

0

This error arises when you pass in a 'skip' value to the query that is less than 0. I would recommend printing out the value you are passing in as 'skip' to your 'factory' function to confirm.

Not knowing your data, or where that specific line is called, I cannot say exactly why a negative number is being passed in, but that is the cause of the error.

shelman
  • 2,689
  • 15
  • 17
  • The error arises when the as_array() function of Mango Iterator class is called, The iterator has data and there is no 'skip' value passed to the 'factory' function. – YAAK Sep 28 '12 at 02:30
0

I have the same issue!

Error code:

let { page, limit }: any = req.query

if (!page) page = 1
if (!limit) limit = 12

const limiter = parseInt(limit)
const skip = (page - 1) * limit

and when I go to /?page=0 I got an error! To fix this follow the step bellow

Fixed:

const limiter = parseInt(limit)
const skip = page > 0 ? (page - 1) * limit : 0

It's works!