MongoDB_排序

'use strict';

// connect MongoDB
var mongodb = require("mongodb");
var server = new mongodb.Server("127.0.0.1", 27017, {auto_reconnect:true});
var client = new mongodb.Db("testDB", server, {w:1});

client.open(function(err, db){
    if (err)
    {
        throw err;
    }
    else
    {
        db.collection("testTable", {safe:true}, function(err, collection){
            if (err)
            {
                throw err;
            }
            else
            {
                // 全部查找
                collection.find().toArray(function(err, result){
                    //JsonLog(result);
                });

                // 1 为升序排列,-1 为降序排列
                collection.find({}, {name:1, age:1, _id:0}).sort({age:1}).toArray(function(er, result){
                    JsonLog(result);
                });
            }
        });
    }
});

你可能感兴趣的:(MongoDB数据库)