0

Below is the mongoose code in app.js

app.get('/view', function (req, res) {


if (err) return handleError(err);

    {

); });

I have two different host entries that is Host_name:Redhat1,Host_name:Redhat2. How to get only latest entries of both using mongoose. What should be modified in the above find condition in schema. Please help

joy
  • 9
  • 5

2 Answers2

1

Simplest way : find by Host_Name (one query for each name), order desc by the timestamp and limit to 1.

// needed to tell mongoose to use ES6 Promises
require('mongoose').Promise = global.Promise;

app.get('/view', function (req, res) {
    var query1 = User.find({ "Host.Host_name": "RedHat1" })
        .sort({ Date.Time_stamp: -1 })
        .limit(1)
        .exec(); // exec() without argument returns a promise

    var query2 = User.find({ "Host.Host_name": "RedHat2" })
        .sort({ Date.Time_stamp: -1 })
        .limit(1)
        .exec();

    Promise.all([query1, query2])
        .then(function (docs) {
            var Users = docs.map(function (User){
                return {
                    time: User.Date.Time_stamp,
                    host: User.Host.Host_name
                }
            });
            res.render('index',{Users: Users});
        }).catch(function (err) {
            console.log(err);
        });
});

Edit: doing two queries leads to an another issue, you have to wait for both to be complete before sendind a response so I updated my example.

Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • Corrected few typos. Try `console.log(docs)`, I just tried, it works. `docs` is an array containing two arrays, each of these array contains one document. – Shanoor Oct 26 '15 at 05:46
  • No, it's the standard ES6 promises, it's around for a while. The issue is elsewhere, what version of mongoose are you using? Maybe it's so old it doesn't return a promise (it seems to require a callback for exec). [Doc for `exec()` function](http://mongoosejs.com/docs/api.html#query_Query-exec). – Shanoor Oct 26 '15 at 06:46
  • Ouch, it's a 4 years old version, you should consider updating if you can. – Shanoor Oct 26 '15 at 07:55
  • But how did you get that old version? How did you install mongoose? Running `npm install mongoose` in your project directory will install the latest version. – Shanoor Oct 26 '15 at 07:59
  • See my comment below to *Alok Deshwal*'s answer, I posted a code that should work. – Shanoor Oct 26 '15 at 08:09
1

try this one

User.aggregate(
        { $match: { "Host.Host_name": { $in: ["RedHat1", "RedHat2"] }}}
        { $group: { _id: "$Host.Host_name", Time_stamp:{$last:'$Date.Time_stamp'}}}
       ,function (err,docs) {
        if (err) return handleError(err);
                var Users = docs.map(function (User){
                    return {
                        time: User.Time_stamp,
                        host: User._id
                    }
                });
            res.render('index',{Users: Users});
        });

Hope this will resolve your problem

Alok Deshwal
  • 1,128
  • 9
  • 20