-2
"tasks" : {
    "details" : [
        {
            "task" : "a",
            "hours" : 5,
            "_id" : ObjectId("6333fdc699f7cb3da66be656")
        },
        {
            "task" : "2",
            "hours" : 5,
            "_id" : ObjectId("6335ed42115208d81dffd082")
        },
        {
            "task" : "b",
            "hours" : 6,
            "_id" : ObjectId("6335ed7d115208d81dffd118")
        }
    ]
}

I am trying to get the totals of "hours" with reduce function but I cannot make it right. Someone can help with this since I'm new to javascript?

  • 2
    let's see your reduce function! – gog Sep 30 '22 at 07:24
  • Please [edit] your question to include your attempt at using the `reduce` function, otherwise we can't help you getting it right – Bergi Sep 30 '22 at 07:26
  • what you have done so far. – Mohit Sharma Sep 30 '22 at 07:27
  • That closing link is wrong, it's https://stackoverflow.com/q/72408627/2813224 – zer00ne Sep 30 '22 at 07:31
  • @zer00ne you can edit the duplicate targets with edit button in the top right – adiga Sep 30 '22 at 07:51
  • first I was using aggr. like this exports.getTotals = expressAsync(async (req, res) => { const query = [ { $match:{} }, { $group: { _id: '$hours', hours: {$sum: '$tasks.details.hours'}, totalHrs: {$sum: 1}, } } ]; const data = await MSPQuotation.aggregate(query); let hours = 0; let totalHrs = 0; for(const item of data){ hours+=item.hours; totalHrs+=item.totalHrs; } return res.json({ code: 200, data: data, count: data.length,hours,totalHrs, enums: {} }); }) – Christian Sep 30 '22 at 08:05
  • But I need to query the totals hours. Can somehelp me how to query "hours" from mongo? – Christian Sep 30 '22 at 08:06
  • @adiga, thanks, 9 years on this site and I never noticed that. It looks like someone already edited it to point to a more appropriate duplicate post. – zer00ne Sep 30 '22 at 08:20
  • @zer00ne yeah, I edited that (FYI: This info is available in the [Post timeline](https://stackoverflow.com/posts/73905291/timeline)). The option is only available if you have the dupe hammer. – adiga Sep 30 '22 at 09:44

1 Answers1

0

const tasks =  {
    "details" : [
        {
            "task" : "a",
            "hours" : 5,
            "_id" : ObjectId("6333fdc699f7cb3da66be656")
        },
        {
            "task" : "2",
            "hours" : 5,
            "_id" : ObjectId("6335ed42115208d81dffd082")
        },
        {
            "task" : "b",
            "hours" : 6,
            "_id" : ObjectId("6335ed7d115208d81dffd118")
        }
    ]
}
const result = tasks.details.reduce((acc, cur) => acc + +cur.hours, 0);
console.log(result);
Ghost
  • 207
  • 1
  • 5
  • thanks a lot man. how can I query the totals of hours in mongodb? – Christian Sep 30 '22 at 07:54
  • ofc you are using mongoDB, I believe they have some things call middleware hook, you can apply one for .find (or whatever you use). Another options is view this https://stackoverflow.com/questions/18969916/mongodb-sum-query – Ghost Sep 30 '22 at 08:08
  • I cannot query hours only for some reason :/ – Christian Sep 30 '22 at 08:39