日期:2014-05-16  浏览次数:20523 次

Mongodb+nodejs复杂查询实例笔记

?

var MONGO  = require("mongodb"),
    server = new MONGO.Server("127.0.0.1",27017),
    client = new MONGO.Db('notices',server,{safe:true});
client.open(function(err){
   if(!err){
        client.collection('notices',function(err,collection){
            if(err){
                console.log("collection error");
                client.close();
                return;
            }
            collection.find(
                {a:{$lt:9,$gte:1,$exists:true}},
                {sort:{a:-1,_id:1},skip:2,limit:3},
                function(err,results){
                    if(err){
                        client.close();
                        console.log("collection find error");
                        return;
                    }
                    results.toArray(function(err,arr){
                        client.close();
                        if(err){
                            console.log("results toArray error");
                            return;
                        }
                        console.log(arr);
                        process.exit();
                    });
            });
        });
   }
   else
   {
        console.log("db open error");
   }
});

?

author: mooring

site:mooring.iteye.com

date:2012/11/07