Mongoose aggregate returning empty result [duplicate] - node.js

This question already has answers here:
Moongoose aggregate $match does not match id's
(5 answers)
Closed 3 years ago.
I have problems with a mongoose aggregate request.
It's kind of driving me crazy cause I can't find a solution anywhere. I would be very grateful for any support.
The schema:
var EvalSchema = new Schema({
modified: {type: Date, default: Date.now},
created : {type: Date, default: Date.now},
username: {type: String, required: true},
item: {type: String, required: true},
criteria: [{
description: {
type: String
},
eval: {
type: Number
}
}]
});
mongoose.model('Eval', EvalSchema);
and I'm using an aggregation to compute the sum of evaluations for each criterion for a given item.
Eval.aggregate([{
$match: {
item: item.id
}
}, {
$unwind: "$criteria"
}, {
$group: {
_id: "$criteria.description",
total: {
$sum: "$criteria.eval"
},
count: {
$sum: 1
}
}
}, {
$project: {
total: 1,
count: 1,
value: {
$divide: ["$total", "$count"]
}
}
}], function(err, result) {
if (err) {
console.log(err);
}
console.log(result);
});
Result is always empty....
I'm logging all queries that mongoose fire in the application. When I run the query in Mongodb, it returns the correct result.
coll.aggregate([{
'$match': {
item: 'kkkkkkkkkkk'
}
}, {
'$unwind': '$criteria'
}, {
'$group': {
_id: '$criteria.description',
total: {
'$sum': '$criteria.eval'
},
count: {
'$sum': 1
}
}
}, {
'$project': {
total: 1,
count: 1,
value: {
'$divide': ['$total', '$count']
}
}
}])
Result:
{
result: [{
"_id": "Overall satisfaction",
"total": 4,
"count": 1,
"value": 4
}, {
"_id": "service",
"total": 3,
"count": 1,
"value": 3
}, {
"_id": "Quality",
"total": 2,
"count": 1,
"value": 2
}, {
"_id": "Price",
"total": 1,
"count": 1,
"value": 1
}],
ok: 1
}
The model is referencing the correct collection.
Thank you :)

Your item.id in the $match function is a String, therefore you will need to convert it to an ObjectID, like so:
$match: { item: mongoose.Types.ObjectId(item.id) }
You can refer to this issue on GitHub aggregate for more details.

Related

How to create mongodb aggregation pipeline between two collections?

I want to create a Mongodb aggregation pipeline for a collection named Transaction.
The Transaction collection has values amount, categoryID, description and I also have a Category collection with values type, icon and color.
I want the pipeline to show the top3 categories with their percentage values and a others category with its percentage value.
the transaction type should be Expense which it should get from the Category collection and it should show all transactions having category type Expense. The top3 should then give the results as transaction with category (example)
type : Rent
percentage:45
type: Entertainment
percentage: 30
type: Food
percentage: 20
type: Others
percentage: 5
I tried it with Category collection but I don't want category to store amount, but Transaction should store amount.
Category.aggregate([
{
$match: {
type: 'expense'
}
},
{
$group: {
_id: "$name",
amount: { $sum: "$amount" }
}
},
{
$group: {
_id: null,
totalExpense: { $sum: "$amount" },
categories: {
$push: {
name: "$_id",
amount: "$amount"
}
}
}
},
{
$project: {
_id: 0,
categories: {
$map: {
input: "$categories",
as: "category",
in: {
name: "$$category.name",
percent: { $multiply: [{ $divide: ["$$category.amount", "$totalExpense"] }, 100] }
}
}
}
}
},
{
$unwind: "$categories"
},
{
$sort: { "categories.percent": -1 }
},
{
$limit: 3
}
])
This was the pipeline I used for it.
//edit
Tried the method suggested by Joe
Transaction.aggregate([
// Join the Transaction collection with the Category collection
{
$lookup: {
from: 'Category',
localField: 'categoryID',
foreignField: '_id',
as: 'category',
},
},
// Unwind the category array to separate documents
{
$unwind: '$category',
},
// Filter for transactions where the category type is "Expense"
{
$match: {
'category.type': 'Expense',
},
},
// Group transactions by category type and calculate the percentage
{
$group: {
_id: '$category.type',
total: { $sum: '$amount' },
count: { $sum: 1 },
},
},
{
$project: {
_id: 0,
category: '$_id',
percentage: {
$multiply: [{ $divide: ['$count', { $sum: '$count' }] }, 100],
},
},
},
// Sort the categories by percentage in descending order
{
$sort: { percentage: -1 },
},
// Limit the result to top 3 categories
{
$limit: 3,
},
// group the rest of the categories as others
{
$group: {
_id: null,
top3: { $push: '$$ROOT' },
others: { $sum: { $subtract: [100, { $sum: '$top3.percentage' }] } },
},
},
{
$project: {
top3: 1,
others: { category: 'Others', percentage: '$others' },
},
},
]);
I am getting an empty array rather than the values. I have data in the collections with the correct ID's. What might be the issue?
//Answer
This aggregation worked for me
Transaction.aggregate([
{
$match: {
userID: { $eq: UserID },
type: 'Expense',
},
},
{
$addFields: { categoryID: { $toObjectId: '$categoryID' } },
},
{
$lookup: {
from: 'categories',
localField: 'categoryID',
foreignField: '_id',
as: 'category_info',
},
},
{
$unwind: '$category_info',
},
{
$group: {
_id: '$category_info.name',
amount: { $sum: '$amount' },
},
},
{
$sort: {
amount: -1,
},
},
{
$group: {
_id: null,
total: { $sum: '$amount' },
data: { $push: '$$ROOT' },
},
},
{
$project: {
results: {
$map: {
input: {
$slice: ['$data', 3],
},
in: {
category: '$$this._id',
percentage: {
$round: {
$multiply: [{ $divide: ['$$this.amount', '$total'] }, 100],
},
},
},
},
},
others: {
$cond: {
if: { $gt: [{ $size: '$data' }, 3] },
then: {
amount: {
$subtract: [
'$total',
{
$sum: {
$slice: ['$data.amount', 3],
},
},
],
},
percentage: {
$round: {
$multiply: [
{
$divide: [
{
$subtract: [
'$total',
{ $sum: { $slice: ['$data.amount', 3] } },
],
},
'$total',
],
},
100,
],
},
},
},
else: {
amount: null,
percentage: null,
},
},
},
},
},
]);

I am trying to get retrive data from mongodb but not getting expected output

DB Data -
[{
title: "Vivo X50",
category: "mobile",
amount: 35000
},
{
title: "Samsung M32",
category: "mobile",
amount: 18000
},
{
title: "Lenovo 15E253",
category: "laptop",
amount: 85000
},
{
title: "Dell XPS 15R",
category: "laptop",
amount: 115000
}]
Expected Output:
[{
category: "mobile",
qty: 2,
totalAmount: 53000
},
{
category: "laptop",
qty: 2,
totalAmount: 200000
}]
Code I am running (Using mongoose)
let products = await Product.aggregate([
{
$project: { _id: 0, category: 1, amount: 1 },
},
{
$group: {
_id: "$category",
qty: { $sum: 1 },
totalAmount: { $sum: "$amount" },
},
},
]);
Result I am Getting.
[
{
"_id": "laptop",
"count": 2,
"totalSum": 200000
},
{
"_id": "mobile",
"count": 2,
"totalSum": 53000
}
]
As you can clearly see that I am able to get correct data but I want correct name also category instead of _id. Please help me with that. Thanks in advance
You need $project as the last stage to decorate the output document.
{
$project: {
_id: 0,
category: "$_id",
qty: "$qty",
totalAmount: "$totalAmount"
}
}
Meanwhile, the first stage $project doesn't need.
db.collection.aggregate([
{
$group: {
_id: "$category",
qty: {
$sum: 1
},
totalAmount: {
$sum: "$amount"
}
}
},
{
$project: {
_id: 0,
category: "$_id",
qty: "$qty",
totalAmount: "$totalAmount"
}
}
])
Sample Mongo Playground
You can use the following query to get your expected output. cheers~
await Product.aggregate([
{
$group: {
_id: "$category",
qty: {
$sum: 1
},
totalAmount: {
$sum: "$amount"
},
},
},
{
$addFields: {
category: "$_id"
}
},
{
$project: {
_id: 0
},
}
])

Count by category and sum it up in MongoDB

I have a product collection in MongoDb which sole fields like _id, category, user_id.
I want to check and count the sum number of each category in collection given the matching the user_id and then sum up all the count again at the end.
my solution is :
return Product.aggregate([
{ $match: { "user_id": "id if user that added the product" } },
{ "$unwind": "$category" },
{
"$group": {
"_id": {
'category': '$category',
},
"count": { "$sum": 1 }
}
},
{ "$sort": { "_id.category": 1 } },
{
"$group": {
"_id": "$_id.category",
"count": { "$first": "$count" }
}
}
])
the code gives me the count of each category without matching the condition of user_id. But when I add the $match it fails.
Product Schema:
const ProductSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
quantity: {
type: Number,
default: -1
},
category:
{
type: String,
required: true
},
manufactured_by: {
type: String,
required: true
},
user_id: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true
}
})
my result if I dont add the condition:
[
{
"_id": "A Tables",
"count": 1
},
{
"_id": "C Tables",
"count": 4
},
{
"_id": "B Tables",
"count": 2
}
]
Not sure what you are trying to achieve from the last stage in your pipeline.
But the following should give you desired output (without any complications that you added)
async getSellerStatistics(seller_id) {
return await Product.aggregate([
{ $match: { user_id: seller_id } }
{ $unwind: "$category" },
{
$group: {
_id: "$category",
count: { $sum: 1 },
},
},
{ $sort: { _id: 1 } },
])
}

sort on object refering element field not working with $group and $project in Mongodb

I am getting this output:
Sorting on the inner array is not working. I have the two tables as shown below.
The pages schema is this:
const PageSchema = new Schema({
name: {
type: String,
required: true
},
created: {
type: Date
},
position: {
type: Number,
default: 0
}
});
module.exports = mongoose.model('pages', PageSchema);
The container schema is this:
const ContainerSchema = new Schema({
filename: {
type: String,
required: true
},pageId: {
type: Schema.Types.ObjectId,
ref: 'pages'
},
created: {
type: Date
}
});
For sorting the data I used this code:
Container.aggregate(match, {
"$group": {
"_id": {
"pageId": "$pageId",
"id": "$_id",
"filename": "$filename",
"position": "$position"
},
"containerCount": {
"$sum": 1
}
}
}, {
"$group": {
"_id": "$_id.pageId",
"container": {
"$push": {
"_id": "$_id.id",
"filename": "$_id.filename",
},
},
"position": {
"$first": "$_id.pageId.position"
}
"count": {
"$sum": "$containerCount"
}
}
}, {
"$project": {
"container": 1,
"count": 1
}
}, {
"$sort": {
"position": 1
}
}).exec()
I want the data sort according to the position field in the pages but it's not working.
You have forgotten to add position in $project.
Once you add in $project then its available in $sort
{
"$project": {
"position" :1,
"container": 1,
"count": 1
}
}

Aggregate results, sum only if condition is true

I'm trying to get a sum of all values of $revenue, and a count of only where $user is equal to the user param I pass when calling this function.
this.aggregate([
{ $match: { createdAt: { $gte: start, $lte: end }, 'status.verified': true } },
{
$group: {
_id: null,
balance: {
$sum: "$revenue"
},
count: {
$cond: {
if: { $eq: [ "$user", user ] },
then: { $sum: 1 },
else: { $sum: 0 }
}
}
}
}
], next);
I'm expecting the data to look like this:
[ { _id: null, balance: 1287, count: 10 ] }
Where balance is the sum of all revenue fields in the match query, and count is the count of that users contributions to the data.
It works fine if I sum the count unconditionally (e.g. like this)
this.aggregate([
{ $match: { createdAt: { $gte: start, $lte: end }, 'status.verified': true } },
{
$group: {
_id: null,
balance: {
$sum: "$revenue"
},
count: { $sum: 1 }
}
}
], next);
Which suggests the error is with my conditional sum. The error thrown by MongoDB is
TypeError: Cannot read property '0' of undefined
at redacted:10:20
at redacted/node_modules/mongoose/lib/aggregate.js:529:13
My schema is
var schema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
status: {
verified: { type: Boolean, default: false },
completed: { type: Boolean, default: false },
canceled: { type: Boolean, default: false },
refused: { type: Boolean, default: false }
},
meta: { type: Schema.Types.Mixed, default: {} },
revenue: { type: Number, default: 0 }
});
Note: the createdAt value used in $match is inserted automatically by a plugin.
The $cond operator should essentially be an expression of the $sum operator, like the following:
this.aggregate([
{ "$match": {
"createdAt": { "$gte": start, "$lte": end },
"status.verified": true
} },
{
"$group": {
"_id": null,
"balance": { "$sum": "$revenue" },
"count": {
"$sum": {
"$cond": [
{ "$eq": [ "$user", user ] },
1, 0
]
}
}
}
}
], next);

Resources