Skip to content

Odd problem with cursor.count #189

@jojobyte

Description

@jojobyte

Not sure if this is a mongoskin or mongodb problem, but as I'm using mongoskin it might be helpful for others as I couldn't find a reference to this.

Had a server running Mongo 2.6 and an index was failing, found out we had to upgrade to 3.2 to get the index to work properly. Did the upgrade, and we then had to upgrade the drivers.

We're running these versions now:

    "mongodb": "^2.0.55",
    "mongoskin": "^2.0.3",

After the upgrade, a binding we had that uses cursor.count stopped functioning properly.

The original code:

db.bind('stuff').bind({
    getByAccount: function(acctId, paginate, callback) {
        var cursor = this.find({
                account: ObjectID(acctId)
            })
            .limit(paginate.limit)
            .sort({ _id: -1 })
            .skip(paginate.page - 1 > 0 ? (paginate.page - 1 * paginate.limit) : 0);

        cursor.count(function(countErr, countRes) {
            var opData = {
                page: paginate.page,
                total: Math.ceil(countRes / paginate.limit),
                count: countRes,
                limit: paginate.limit
            };
            cursor.toArray(function(arrErr, arrRes) {
                opData.results = arrRes;
                callback(null, opData);
            });
        });
    }
});

The fix:

        // add false before cursor callback
        cursor.count(false, function(countErr, countRes) {
            // ...
        });

As far as I can tell this should be the default, even after the upgrade, but for some reason this call is coming back with a true value when left undefined for applySkipLimit

My concern is that this is may be the wrong way to override applySkipLimit and might later become deprecated or invalid.

Any thoughts? Is this a MongoDB Driver change or MongoSkin?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions