-6

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();
            }
        });*/

        }
});
kissmark
  • 23
  • 4
  • 1
    Recommended reading: https://docs.mongodb.org/manual/reference/sql-comparison/ As is this: https://docs.mongodb.org/manual/reference/sql-aggregation-comparison/ – Blakes Seven Nov 18 '15 at 13:19

1 Answers1

1

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

Community
  • 1
  • 1
Trung
  • 1,372
  • 2
  • 14
  • 21
  • Record.find([{ 'ip': ipAddress},{ 'filename_full': filename}]) .sort({'pieces':-1}) .limit(1) .exec(function (err, recordFinal) { if (err){ res.send(err); console.log("PIECES: ",recordFinal.pieces); }); – kissmark Nov 18 '15 at 15:27
  • but it give me an object with the full details not just the pieces...how can i do that it is just give me the pieces integer? – kissmark Nov 18 '15 at 15:28
  • and write that the recordFinal.pieces is undefined – kissmark Nov 18 '15 at 15:30
  • Use `findOne` here instead of `find`, otherwise `doc` is an array instead of the single doc you're looking for. – JohnnyHK Nov 18 '15 at 15:51
  • Thank @JohnnyHK for your suggestion. – Trung Nov 18 '15 at 16:11