简体   繁体   中英

How can i write in NodeJS with MongoDB the following MySQL Select?

What i want to:

SELECT MAX(pieces) //(to a var variable)
FROM record
WHERE ip = :ipAddress AND filename_full = :filename

:ipAddress and filename come via the http...Thanks for the help

the db

where I want to use:

 mongoose.connect('mongodb://localhost:27017/newDB', function(err,db){
    if(!err){
        acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_'));
        acl.allow('guest', 'business', 'view');

        console.log(acl);
        var cursor = db.collection('record').find({ip: ipAddress, filename_full: filename});
        cursor.each(function (err, doc) {
            assert.equal(err, null);
            if (doc != null) {
                console.dir(doc);
            } else {
                console.log("Not found");
                callback();
            }
        });*/

        }
});

You can use Mongoose , it help you to work with MongoDB easier.

Record.findOne({
    ip: ipAdress,
    filename_full: filename
}).sort({
    'pieces': -1
}).exec(function(err, doc) {

});

Reference: Mongoose doc , Get max value in mongoose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM