过滤查询的mongodb本机驱动程序问题

时间:2016-02-19 15:10:24

标签: node.js mongodb

我无法使用查询mongodb。

我不知道我是否可以详细说明所使用的方法是否正在发送一个对象,但是被拒绝或有时过滤器返回空。

它总是给我一个错误:

MongoError: query selector must be an object

因此查询的条目是:

{ filter: 'f', limit: '5', page: '1', sort: '-posted' }

我的服务器路线:

exports.findAll = function(req, res, next) {

var locals  = {},
    section = req.params.section,
    query   = req.query,
    filter  = {};

if(query.filter) {
    filter = query.filter.replace(/"(\w+)"\s*:/g, '$1:');
    filter = filter.replace(/["]/g, "'");
}

console.log(filter);


delete query.filter;

async.series([

    function(callback) {
        MongoClient.connect(url, function(err, db) {
            if (err) return callback(err);
            locals.collection = db.collection(section);
            callback();
        });
    },

    function(callback) {
        locals.collection.count(filter, function (err, result){
            if (err) return callback(err);
            locals.count = result;
            callback();
        });
    },

    function(callback) {

        //var cursor = locals.collection.find(filter);
        var cursor = locals.collection.find(filter, req.query);

        // if(req.query.order) {
        //  cursor = cursor.sort();
        // }
        //
        // if(req.query.limit) {
        //  cursor = cursor.limit(Math.abs(req.query.limit));
        //
            if(req.query.page) {
                cursor = cursor.skip(Math.abs(req.query.limit) * --req.query.page);
            }
        // }

        cursor.toArray(function(err, docs) {
            if (err) return callback(err);
            locals.docs = docs;
            callback();
        });
    }
],

function(err) { //This function gets called after the three tasks have called their "task callbacks"
    if (err) return next(err);
    // Here locals will be populated with 'count' and 'docs'
    res.json({
        count: locals.count,
        data: locals.docs
    });
});

};

我改变了功能服务器,现在我的回答是:

{ filter: {situation: 't'}, limit: '5', page: '1', sort: '-posted' }

同样的错误:( :(

0 个答案:

没有答案