How can I retrieve current week's data on Mongoose? - mongodb

I have a Model for all requests that I get for my business (real estate).
I want to create an aggregation middleware based on the "createdAt" field which returns only models created between monday at midnight and the present time.
I have looked through all previous questions but could not find anything and the aggregation docs in mongo are so huge I cannot wrap my head around!
Do you have any tips?
I have tried this but it's returning an empty array:
getGestionaleStats = async (req, res) => {
try {
const richieste = await Richiesta.aggregate([
{
$match: {
createdAt: { $lt: new Date() }
}
},
{
$group: {
_id: { $week: '$createdAt' }
}
}
]);
res.status(200).json({
status: 'success',
data: { richieste }
});
} catch (err) {
console.error(err.message);
res.status(500).json({
status: 'error',
data: err.message
});
}
Of course the {$lt: new Date()} is probably where the problem lies.
Any suggestions?

This solution uses Aggregation query and a custom JavaScript function. The function takes a date object and returns the first Monday's date before today. This is used to get all the documents with createdAt date after the calculated date.
// Function returns the date of the "last Monday" from
// the given input date.
function getLastMonday(dt) {
let n = null; // last Monday conversion
switch (dt.getDay()) {
case 0: n = -5; break;
case 1: n = -6; break;
case 2: n = 0; break;
case 3: n = -1; break;
case 4: n = -2; break;
case 5: n = -3; break;
case 6: n = -4; break;
default: "This never happens";
}
let today_date = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
let last_monday_date = today_date.setDate(today_date.getDate() + n );
return last_monday_date;
}
var d = ISODate(); // -or- any date like ISODate("2019-11-26T00:00:00Z")
var LAST_MONDAY = getLastMonday(d);
db.test.aggregate( [
{
$addFields: {
last_monday: {
$dateFromParts : {
year: { $year: new Date(LAST_MONDAY) },
month: { $month: new Date(LAST_MONDAY) },
day: { $dayOfMonth: new Date(LAST_MONDAY) }
}
},
created_at: {
$dateFromParts : {
year: { $year: "$createdAt" },
month: { $month: "$createdAt" },
day: { $dayOfMonth: "$createdAt" }
}
}
}
},
{
$match: { $expr: { $gt: [ "$created_at", "$last_monday" ] } }
},
{
$project: { created_at: 0, last_monday: 0 }
}
] )
For a set of input documents like this:
{ _id : 1, createdAt : ISODate("2019-12-03T00:00:00Z") }
{ _id : 2, createdAt : ISODate("2019-11-12T02:00:00Z") }
{ _id : 3, createdAt : ISODate("2019-11-25T05:00:00Z") }
{ _id : 4, createdAt : ISODate("2019-11-26T00:00:00Z") }
{ _id : 9, createdAt : ISODate("2019-12-02T23:45:00Z") }
And, LAST_MONDAY = getLastMonday(ISODate("2019-12-04T05:40:20Z")), the aggregation query returns the document with _id : 1.

I'm using momentJS for this:
const result = Collection.aggregate([
{
$match: {
createdAt: {
$gte: moment().startOf('isoweek').toDate(),
$lt: moment().endOf('isoweek').toDate()
},
}
}
]);

I've found an answer with vanillaJS:
const richieste = await Richiesta.aggregate([
{
$match: {
createdAt: { $gte: getBeginningOfTheWeek(new Date()), $lt: new Date() }
}
},
{
$group: {
_id: null,
count: { $sum: 1 }
}
},
]}
where getBeginningOfTheWeek is as such:
exports.getBeginningOfTheWeek = (now) => {
const days = (now.getDay() + 7 - 1) % 7;
now.setDate(now.getDate() - days);
now.setHours(0, 0, 0, 0);
return now;
};
The latter function is from T.J. Crowder: get current week moday javascript

Related

mongodb aggregation where document field is less than another field

Using mongoose, I'm trying to make a query that searches for tasks where timeSpent is greater than timeBilled.
Task schema:
const myTaskSchema = new Schema({
date: { type: Date, default: Date.now },
timeSpent: { type: Number },
timeBilled: { type: Number }
})
The query I've tried:
myTaskSchema.aggregate([
{
$match: {
timeSpent: { $gt: '$timeBilled' }
}
}
])
.then(data => {
console.log(data)
})
But I'm getting zero results (I know there should be results)
NOTE: Not every task has a timeSpent or timeBilled.field if that matters.
here is my dirty solution. It'd be nice if I didnt have to add a field but this gets me where I want to be.
myTaskSchema.aggregate([
{
$addFields: {
needToBill: { $gt: ['$timeSpent', '$timeBilled'] }
}
},
{
$match: {
needToBill: true
}
},
{
$project: {
timeSpent: 1,
timeBilled: 1
}
}
])

Express, Mongodb (mongoose) - Data comparison and count

In my mongodb database i have datas like:
{ id: 'ran1', code: 'ABC1', createdAt: 'Sep 1 2022', count: 5 }
{ id: 'ran2', code: 'ABC1', createdAt: 'Sep 2 2022', count: 3 }
{ id: 'ran3', code: 'ABC2', createdAt: 'Sep 1 2022', count: 2 }
{ id: 'ran4', code: 'ABC1', createdAt: 'Oct 1 2022', count: 1 }
{ id: 'ran5', code: 'ABC1', createdAt: 'Oct 2 2022', count: 2 }
{ id: 'ran6', code: 'ABC2', createdAt: 'Ocr 1 2022', count: 1 }
now as an output i want all the data from October but i also want to count and compare the percentage.
So the output for October will be
{code: 'ABC1', totalCount: the sum of total count of oct (1+2) =3 , percent: (total count of oct - total count of sep)/total count of oct * 100 }
{code: 'ABC2', totalCount: 1, percent: -100}
I tried to achieve these output using two different aggregation and later map the current month aggregation with each element from previous month aggregation. But i think there are some better solution.
Here is my code
const { filterDate, shop } = req.query;
const splittedFilter = filterDate.split("-");
const query = {
shopUrl: { $regex: shop, $options: "i" },
createdAt: {
$gte: new Date(splittedFilter[0]),
$lte: new Date(splittedFilter[1]),
},
};
const currentCodes = await BlockedCode.aggregate([
{
$match: query,
},
{
$group: {
_id: "$discountCode",
totalCount: { $sum: "$count" },
},
},
]);
const prevQuery = {
shopUrl: { $regex: shop, $options: "i" },
createdAt: {
$gte: new Date(splittedFilter[2]),
$lte: new Date(splittedFilter[3]),
},
};
const previousCodes = await BlockedCode.aggregate([
{
$match: prevQuery,
},
{
$group: {
_id: "$discountCode",
totalCount: { $sum: "$count" },
},
},
]);
const result = currentCodes.map((code) => {
const foundPrevCode = previousCodes.find((i) => i._id === code._id);
if (foundPrevCode?._id) {
const prevCount = foundPrevCode?.totalCount;
const currCount = code?.totalCount;
const difference = currCount - prevCount;
const percentage = (difference / currCount) * 100;
return { ...code, percentage };
} else {
return { ...code, percentage: 100 };
}
});
#shahamar Rahman i don't understand percentage logic can you explain little more!
upto this i filtered and counted the data based October month,plz check it and let me know if it's helps you
https://mongoplayground.net/p/Fet3UUI9LDC
db.collection.aggregate([
{
$addFields: {
month: {
$month: {
$toDate: "$createdAt"
}
}
}
},
{
$match: {
month: 10
}
},
{
$group: {
_id: {
code: "$code"
},
count: {
$sum: "$count"
}
}
}
])

Statistics with Mongo DB

I have the following DB structure :
{
"uploadedAt": "2021-09-22T22:09:12.133Z",
"paidAt: "2021-09-30T22:09:12.133Z",
"amount": {
"currency": "EUR",
"expected": 70253,
"paid": 0
},
}
I would like to know how do I calculate the total amount that still need to be paid (expected - paid), and the average date between uploadedAt and paidAt. This for multiple records.
My function for getting the data is (the criteria should be updated to get this data).
const invoiceParams = new FindParams();
invoiceParams.criteria = { company: company._id }
const invoices = await this.findAll(invoiceParams);
FindAll function looks like:
async findAll(
params: FindParams,
ability?: Ability,
includeDeleted: boolean = false,
): Promise<Entity[]> {
let queryCriteria: Criteria = params.criteria;
let query: DocumentQuery<Entity[], Entity> = null;
if (!includeDeleted) {
queryCriteria = {
...queryCriteria,
deleted: { $ne: true },
};
}
try {
if (ability) {
ability.throwUnlessCan('read', this.entityModel.modelName);
queryCriteria = {
...toMongoQuery(ability, this.entityModel.modelName),
...queryCriteria,
};
}
query = this.entityModel.find(queryCriteria);
if (params.populate) {
query = query.populate(params.populate);
}
if (params.sort) {
query = query.sort(params.sort);
}
if (params.select) {
query = query.select(params.select);
}
return query.exec();
} catch (error) {
if (error instanceof ForbiddenError) {
throw new ForbiddenException(error.message);
}
throw error;
}
}
Update:
const paymentTime = await this.invoiceModel.aggregate([
{
$group: {
_id: "$account",
averageSpread: { $avg: { $subtract: ["$paidAt", "$uploadedAt"] } },
count: { $sum: 1 }
}
}
]);
Try this aggregation pipeline:
db.invoiceParams.aggregate([
{
$set: {
expectedPaid: { $subtract: ["$amount.expected", "$amount.paid"] },
averageDate: { $toDate: { $avg: [{ $toLong: "$uploadedAt" }, { $toLong: "$paidAt" }] } }
}
}
])

Mongoose aggregate throws error when zero match

I'm trying to aggregate a field in mongodb based on some conditions. This works fine when there's at least single match, but it throws error otherwise. I want to the aggregated value as 0 when there's no match. What need to be modified in below code?
Note: fromDate and toDate passed through API Payload.
"errorMesssage": "Cannot read property 'amount' of undefined"
DTO:
export class MetricsDTO {
fromDate: Date;
toDate: Date;
}
Service
async getRevenue(metricClause: Partial<MetricsDTO>) {
if (Object.keys(metricClause).length == 0) {
var rev = await this.bookingsModel.aggregate([
{ $group: { _id: null, amount: { $sum: '$GrossAmount' } } }
])
} else {
var rev = await this.bookingsModel.aggregate([
{ $match: { TxnDate: { $gte: metricClause.fromDate, $lte: metricClause.toDate } } },
{ $group: { _id: null, amount: { $sum: '$GrossAmount' } } }
])
}
return rev[0].amount;
}
Payload
{
"fromDate": "2019-10-24",
"toDate": "2019-10-25"
}
It is not the aggreage causing the exception, this line causes the error, because you didn't check if the rev is null or not.
return rev[0].amount;
So to handle null, you can:
if (rev && rev[0]) {
return rev[0].amount;
} else {
return 0;
}

MongoDB + Mongoose Aggregate w/ Asnyc

I've got the following route in my express file, which takes parameters passed in from a middleware function and queries my backend MongoDB database. But for some reason, it only ever returns an empty array.
I'd like to convert the Mongoose model that allows me to use aggregate functions into async/await to conform with the rest of my code. It's online here.
module.exports = {
search: asyncWrapper(async(req, res, next) => { // Retrieve and return documents from the database.
const {
filterTarget,
filter,
source,
minDate,
maxDate,
skip,
limit,
sortBy,
sortOrder
} = req.search;
try {
const mongoData = await Model.aggregate([
{
$match: {
date: {
$gt: minDate, // Filter out by time frame...
$lt: maxDate
}
}
},
{
$match: {
[filterTarget]: filter // Match search query....
}
},
{
$set: {
[filterTarget]: { $toLower: `$${filterTarget}` } // Necessary to ensure that sort works properly...
}
},
{
$sort: {
[sortBy]: sortOrder // Sort by date...
}
},
{
$group: {
_id: null,
data: { $push: "$$ROOT" }, // Push each document into the data array.
count: { $sum: 1 }
}
},
{
$project: {
_id: 0,
count: 1,
data: {
$slice: ["$data", skip, limit]
},
}
}
])
return res.status(200).json({ data: mongoData.data || [], count: mongoData.count || 0 });
} catch (err) {
next(err);
}
})
};
For some reason, the route is only returning an empty array every time. I've double and triple checked my variables, they are not the problem.
How can I use the Mongoose.aggregate() function in an async await route?