Mongoose: Populate field in collection from other collection - mongodb

I have 2 Mongoose collections: ExpenseCategory and Expense
var ExpenseCategorySchema = new Schema({
name: String,
totalSpentInThisMonth: Number
});
mongoose.model('ExpenseCategory', ExpenseCategorySchema);
var ExpenseSchema = new Schema({
expenseCategoryId: {type: Schema.Types.ObjectId, ref: 'ExpenseCategory'},
amount: Number,
date: Date
});
mongoose.model('Expense', ExpenseSchema);
There is a GET api call written in Node.js to return all ExpenseCategory items.
appRouter.route('/expensecatgories')
.get(function(req, res){
ExpenseCategory.find({}, function (expenseCategories) {
res.json(expenseCategories);
});
});
In the above GET method I want to populate field totalSpentInThisMonth in each expenseCategories item before returning. This field needs to be calculated as a sum of all expense.amount where expense.expenseCategoryId matched the expenseCategory.id and expense.date is in current month.
How can I populate the field totalSpentInThisMonth before returning expenseCategories?

Use the .aggregate() method from the aggregation framework for this. You would need to first construct dates to use as your date range query for documents whose date falls within the current month, thus you need to calculate
the first and last days of the month date objects. These dates would be used in the $match pipeline to filter out the documents that are not in the current month.
The next pipeline stream would be the $group stage which groups the incoming documents by the expenseCategoryId key so that you may calculate the total spent in the current month using the
accumulator operator $sum.
The following code implements the above:
appRouter.route('/expensecatgories').get(function(req, res){
var today = new Date(), y = today.getFullYear(), m = today.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
var pipeline = [
{
"$match": {
"date": { "$gte": firstDay, "$lt": lastDay }
}
},
{
"$group": {
"_id": "$expenseCategoryId",
"totalSpentInThisMonth": { "$sum": "$amount" }
}
}
];
Expense.aggregate(pipeline, function (err, result){
if (err) throw err;
var categories = result.map(function(doc) { return new ExpenseCategory(doc) });
Expense.populate(categories, { "path": "expenseCategoryId" }, function(err, results) {
if (err) throw err;
console.log(JSON.stringify(results, undefined, 4 ));
res.json(results);
});
});
});

Related

mongoDB Not Returning Query When Trying to Filter By Date

I am trying to find all documents that are created greater than or equal to a month ago.
But when I query the DB it returns nothing when doing the following code:
console.log(moment().add(-31, "days").toDate()) // this logs 2022-09-30T07:27:26.373Z
let filter = {
companyId: companyId,
userId: userId,
_created_at: {$gte: moment().add(-31, "days").toDate()}
};
db.collection("Users")
.find(filter)
.count()
.then(count => {
if(!count){
return resolve({result: [], count: 0});
} else {
db.collection("Users")
.find(filter)
.sort({_created_at: -1})
.limit(parseInt(limit))
.skip(parseInt(page) * parseInt(limit))
.toArray()
.then(result => {
resolve({result: result, count: count});
})
.catch(error => {
console.log(error);
reject(error);
})
}
});
However when I do the following filter it works and returns the documents:
let filter = {
companyId: companyId,
userId: userId,
_created_at: {$gte: moment().add(-31, "days").format("YYYY-MM-DD[T]HH:mm:ss.SSS[Z]") }
};
The only thing i changed is the format of the date and i am specifiying exactly how it should be formatted to equal the DB but i do not want to do something hard coded. Although the moment().add(-31, "days").toDate() matches the same format in my DB.
Why am i not getting any results from the query?
toDate() returns a JS Date object, try with format() to return an ISO formatted date:
let filter = {
companyId: companyId,
userId: userId,
_created_at: { $gte: moment().add(-31, 'days').format() },
};

MongoDB Aggregate Query Returning Empty Array

I'm new to Mongo and am trying to run an aggregate command on my model in my Node.js application (using Express). I'm trying to run the query for finding all users registered within the last month.
When I run User.find(), it returns all 2 users in the DB, so I know the users are definitely there.
However, when I run this, data is just an empty array. Is there a solution I'm missing here?
router.get("/stats", verifyTokenAndAdmin, async (req, res) => {
const date = new Date();
const lastYear = new Date(date.setFullYear(date.getFullYear() - 1));
try {
const data = await User.aggregate([
{ $match: { createdAt: { $gte: lastYear } } },
{
$project: {
month: { $month: "$createdAt" },
},
},
{
$group: {
_id: "$month",
total: { $sum: 1 },
},
},
]);
res.status(200).json(data)
} catch (err) {
res.status(500).json(err);
}
});
Also, the response is a 200 so no errors there.

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?

get first and last values for each condition

I have a collection like this:
{
_id: ObjectId('534343df3232'),
date: ISODate('2016-01-08T00:00:00Z'),
item_type: "book",
book_id: ObjectId('534343df3232fdf'),
user_id: ObjectId('534343df3232fdf23'),
rating: 6
},
{
_id: ObjectId('534343df3232'),
date: ISODate('2016-01-05T00:00:00Z'),
item_type: "movie",
movie_id: ObjectId('534343df3232fdf'),
user_id: ObjectId('534343df3232fdfa'),
rating: 5
},
{
_id: ObjectId('534343df3232'),
date: ISODate('2016-01-010T00:00:00Z'),
item_type: "song",
song_id: ObjectId('534343df3232fdf'),
user_id: ObjectId('534343df3232fdf13'),
rating: 9
}
There can be only one rating per item per user per day.
I would like to check how the ratings evolve between a period of time for a selection of users and items. I need only the first and the last rating for each book/movie/song.
I have no idea on how I could do this the most efficient way.
As for now, I'm retrieving all the ratings for all the users, and then parsing them with PHP.
db.ratings.find({user_id:{$in:[...]}, $or:[book_id:{$in:[...]}, song_id:{$in:[...]}, movie_id:{$in:[...]}, ], date:{$gte:.., $lte..} });
This is obviously unefficient but I don't know how to handle this case.
You can do it with mongodb mapReduce. So at first you need to filter your data on date range, selection of users and selection of items(query part). Then group by item(map part) and for each item select first and last days with corresponding ratings(reduce part).
Try the following query:
var query = {
user_id: {$in:[...]}
date: { $gte: dateFrom, $lt:dateTo},
$or: [
{book_id: {$in:[...]}},
{song_id:{$in:[...]}},
{movie_id:{$in:[...]}}
]
}
var map = function () {
emit(this.item_type, {
first : {rating: this.rating, date: this.date},
last: {rating: this.rating, date: this.date}
})
}
var reduce = function (key, values) {
var res = values[0];
for (var i=1; i<values.length; i++ ) {
if (values[i].first.date < res.first.date)
res.first = values[i].first;
if (values[i].last.date > res.last.date)
res.last = values[i].last;
}
return res;
}
db.collection.mapReduce( map , reduce , { out: { inline : true }, query: query } )

Mongoose returning NULL on findOneAndUpdate()

To learn the MEAN stack (using Mongoose), I'm creating a StackOverflow type application. I have Questions that are stored in Mongo(v3.0.7) and they have Answer sub-documents.
I am trying to increment the Vote of an Answer, but when the question is returned it is null. I'm pretty sure there's something wrong with the query, specifically where I'm trying to get the answer with the ID I need to modify.
Question Schema:
var questionsSchema = new mongoose.Schema({
answers: [ answerSchema ],
});
Answer Schema:
var answerSchema = new mongoose.Schema({
votes: { type: Number, default: 0 },
});
Querying for _id returns null:
Question.findOneAndUpdate(
{_id: req.params.questionId, 'answers._id': req.params.answerId },
{ $inc: { 'answers.$.votes': 1 } },
{ new: true },
function(err, question){
if (err) { return next(err); }
//question is returned as NULL
res.json(question);
});
Querying for 0 votes works:
Question.findOneAndUpdate(
{_id: req.params.questionId, 'answers.votes': 0 },
{ $inc: { 'answers.$.votes': 1 } },
{ new: true },
function(err, question){
if (err) { return next(err); }
//question is returned as NULL
res.json(question);
});
UPDATE:
Query through Mongo the result is returned:
db.questions.find({_id: ObjectId('562e635b9f4d61ec1e0ed953'), 'answers._id': ObjectId('562e63719f4d61ec1e0ed954') })
BUT, through Mongoose, NULL is returned:
Question.find(
{_id: Schema.ObjectId('562e635b9f4d61ec1e0ed953'), 'answers._id': Schema.ObjectId('562e63719f4d61ec1e0ed954') },
Try to use mongoose Types ObjectID
http://mongoosejs.com/docs/api.html#types-objectid-js:
var ObjectId = mongoose.Types.ObjectId;
Question.find({
_id: '562e635b9f4d61ec1e0ed953',
'answers._id': new ObjectId('562e63719f4d61ec1e0ed954')
})
Final answer to the original update question:
Question.findOneAndUpdate(
{_id: req.params.questionId,
'answers._id': new ObjectId(req.params.answerId) },
{ $inc: { 'answers.$.votes': 1 } },
{ new: true },
function(err, question){
if (err) { return next(err); }
res.json(question);
});
You don't need ObjectId at all:
Question.findOne({_id: "562e635b9f4d61ec1e0ed953"}, callback)
Mongoose handles the string for you.
In addition, using find() and querying by _id will result in an array of length 0 or 1. Using findOne() will return the document object.