Add aggregation by the condition of the value of your own variable

Alexander Laktionov

I have two functions to get an array from monga using aggregation. The functions are completely the same, except for one pipeline - "match ({startTime: {$ gte: start}})". How can I leave one function and add a "math" only by the presence of the "start" variable, which is a date filter?

 let groupedByUserSessions
    if (lastDay) {
        const start = getDateFromString(lastDay);
        groupedByUserSessions = await getValuesByDate(start)
    } else {
        groupedByUserSessions = await getAllValues();
    }

The functions are completely the same,

function getValuesByDate(start) {
return Sessions.aggregate()
    .match({ startTime: { $gte: start } })
    .group({
        _id: { departament: "$departament", userAdName: "$userAdName" },
        cleanTime: { $sum: { $subtract: ["$commonTime", "$idlingTime"] } }
    })
    .group({
        _id: { departament: "$_id.departament"},
        users: { $push: {value: '$cleanTime', name: '$_id.userAdName'} },
        commonCleanTime: { $sum: "$cleanTime" }
    })
    .project({
        departament: '$_id.departament',
        users: '$users',
        commonCleanTime: '$commonCleanTime',
        performance: { $divide: [ "$commonCleanTime",  { $size: "$users" }] }
    });

}

tromgy

You can break the pipeline into a "head" and a "tail" and construct the head differently when the start argument is given:

function getValuesByDate(start) {
  let agg = Sessions.aggregate();

  if (start) {
    agg = agg.match({ startTime: { $gte: start } });
  }

  return agg
    .group({
      _id: { departament: '$departament', userAdName: '$userAdName' },
      cleanTime: { $sum: { $subtract: ['$commonTime', '$idlingTime'] } },
    })
    .group({
      _id: { departament: '$_id.departament' },
      users: { $push: { value: '$cleanTime', name: '$_id.userAdName' } },
      commonCleanTime: { $sum: '$cleanTime' },
    })
    .project({
      departament: '$_id.departament',
      users: '$users',
      commonCleanTime: '$commonCleanTime',
      performance: { $divide: ['$commonCleanTime', { $size: '$users' }] },
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add a field with increasing value in MongoDB Aggregation based on condition

Is there a way to add your own snippets in google colaboratory

Is it OK to add your own attributes to HTML elements?

Is it possible to add your own strings to a Django SearchVectorField?

Is it possible to add your own WordNet to a library?

add your own custom route to devise

add your own instructions using pin

How to add your own categories into the OneHotEncoder

aggregation of data in R with assign of a dummy variable by condition

Add a value to to the record using aggregation

Why is my variable jumping in value when I add an "if" condition?

I want to add value to multiple variable If condition shorthand

Change value aggregation, if condition node js

C: For loop variable as it's own stopping condition?

The value in the condition without a variable

Add a condition with alias value

ASP.NET Identity UserName (how to add your own verification)?

Is it possible to add your own bookmarks/tabs to a PDF file?

Add a dependency / reference to your own DLL project in Visual Studio 2013

Is it possible to add your own derivable traits, or are these fixed by the compiler?

how to add variable into ActiveRecord :condition => ''

How to add a condition to a variable - GAMS

Changing variable value based on condition

Passing variable value as condition in if statement

Jinja change variable value in IF condition

SQL condition based on variable value

Variable not changing value on checkbox condition

Add value to cell if condition is meet

Project embedded document key value, based on condition in mongoDB aggregation