How sorting leads to finding nodejs-mongodb, but by calling a dynamic method

I am developing a web application in nodes connected to mongodb through my own mongo connector.

In one of my js files, I have a general method for calling the "find" or "findOne" operation to extract everything I need from the mongodb collection, for example:

This works great for me.

But now I need to sort the results, and as far as I know, Mongodb uses the sort method to achieve this.

collection.ensureIndex(indexedFields, function(error, indexName) {
    if (error) {
        callback(error);
    } else {
        var operation = (params.options.one) ? collection.findOne : collection.find;

        operation.call(collection, params.selector, params.fields, params.options,
            function(error, result){
                if (error) {
                    ...
                } else {
                    ... 
                }       
            }
        );
    }
});

In a simple query, it should be something like this: For example:

collection.find().sort({field : 1}),

I do not know how to call the "sort" method, doing this in my general way.

Any ideas?

Thank.

+5
source share
6 answers

I have found a solution.

, "toArray" , .

operation.call(collection, params.selector, params.fields, params.options, function(error, result)   {
if (error) {
    callback(error);
} else {
        result.sort(params.sort).toArray(function(error, items) {
            ... 
        }
}

,

0

mongojs (https://github.com/gett/mongojs)? node, ( expressjs). :

db.test.find().sort({name:1}, function(error, tuples) { console.log(tuples) });
+5

?:

        operation.call(collection, params.selector, params.fields, params.options,
            function(error, result){
                ... 
            }
        ).sort({field: 1});

find, findOne, :

    var operation = (params.options.one) ? collection.findOne : function() {
        var findResults = collection.find.apply(this, [].slice.call(arguments));
        return findResults.sort({field: 1});
    };

(, , , , .)

+1

- , mongodb engine

var grades = db.collection('data');

var cursor = grades.find();
// cursor.skip(1);
// cursor.limit(4);
// cursor.sort(['State', 1]);
cursor.sort( [['Temperature', 'desc'],['State','asc']] );

//var options = { 'skip' : 1,
//                'limit' : 4,
//                'sort' : [['grade', 1], ['student', -1]] };
//var cursor = grades.find({}, {}, options);

cursor.each(function(err, doc) {
    if(err) throw err;
    if(doc == null) {
        return db.close();
    }
    console.dir(doc);
});
+1

, ,

const query = {category: "category1"};
Articles.find(query, callback).sort({created_date: -1});
+1

, .

, sort undefined. , , , .. - , , , findResults.sort undefined.

, , - , , ... - :

operation.call(collection, params.selector, params.fields, params.options,               
   function(error, result){
         if (err) ....
         else results.sort({field:1}, callback(err, sortedResults))
   }

, "results.sort", ... , . mongodb, . , , ...

-;

var sortedResults = results.sort({field:1};
callback(err, sortedResults)

In this case, execution continues, but it returns unsorted results.

Any other ideas? It must be there ....

thank

0
source

All Articles