Reputation: 335
var mongoose = require('mongoose'),
ObjectId = require('mongoose').Types.ObjectId,
Schema = mongoose.Schema;
var teamAttendanceSchema = new Schema({
allMembers: [{type: Schema.Types.ObjectId, ref: 'user' }],
currentDate: {type: Date, default: Date.now()},
team_id : {type: Schema.Types.ObjectId, ref: 'team' },
presentMembers: [{type: Schema.Types.ObjectId, ref: 'user' }],
});
module.exports = mongoose.model('attendance', teamAttendanceSchema);
This is the sample of schema i am using. I want to get the teams in which my user id exists. I can do it by following find query:-
var user_id = req.body.user_id;
TeamAttendance.find({ allMembers : user_id})
But i want to use aggregation with group. I want to get the teams of user with the count of the days he was present. I tried following but it is not working:-
var user_id = req.body.user_id;
TeamAttendance.aggregate(
[
{$match : { allMembers : user_id },
{$group : {
_id : "$team_id"
}}
]).exec()
Upvotes: 0
Views: 69
Reputation: 1
you can Also Use underscore library from npm , after getting result from $match query , then _.groupBy() method of underscore.
Upvotes: 0
Reputation: 1
schedules.aggregate([
{
"$match":
{
"offdeleveryDate.startOffDate": { $lte: new Date(start) },
"offdeleveryDate.endOffDate": { $gte: new Date(end) },
}
}, {
"$match": query
}, {
"$lookup": {
"from": "users",
"localField": "userId",
"foreignField": "_id",
"as": "userInfo"
}
},
{
"$unwind": {
"path": "$userInfo",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "products",
"localField": "productId",
"foreignField": "_id",
"as": "productInfo"
}
},
{
"$unwind": {
"path": "$productInfo",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "services",
"localField": "serviceTypeId",
"foreignField": "_id",
"as": "serviceInfo"
}
},
{
"$unwind": {
"path": "$serviceInfo",
"preserveNullAndEmptyArrays": true
}
},
{
"$project": {
"customerName": 1,
"serviceProviderName": 1,
"createdForID": 1,
"customerPhoneNumber": 1,
"serviceType": "$serviceInfo.serviceType",
"productName": "$productInfo.productName",
"quantity": 1,
"unit": "$productInfo.unit",
"offdeleveryDate.startOffDate": 1,
"offdeleveryDate.endOffDate": 1,
"startOffDate": { '$dateToString': { format: '%Y-%m-%d', date: '$offdeleveryDate.startOffDate' } },
"endOffDate": { '$dateToString': { format: '%Y-%m-%d', date: '$offdeleveryDate.endOffDate' } }
}
},
{
"$group": {
"_id": {
"customerID": "$createdForID",
"customerName": "$customerName",
"customerPhoneNumber": "$customerPhoneNumber",
"serviceProviderName": "$serviceProviderName"
},
"customerDetails": {
"$push": {
"serviceType": "$serviceType",
"productName": "$productName",
"quantity": "$quantity",
"unit": "$unit",
"startOffDate": "$startOffDate",
"endOffDate": "$endOffDate"
}
}
}
}
], function (err, data) {
if (err) {
return reply(Boom.forbidden(err));
} else {
if (data && data.length) {
return reply(Common.successResponse('Success', data));
} else {
return reply(Common.successResponse('No entry available for this date.'));
}
}
});
});
}
Upvotes: 0
Reputation: 1274
In aggregate try $in and make sure userId is ObjectId, I think the main issue of your aggregate pipeline maybe userid is in the string but aggregate needs mongoose object id to work try below:
let mongoose = require('mongoose');
var user_id = mongoose.Types.ObjectId(req.body.user_id);
TeamAttendance.aggregate(
[
{$match : { allMembers : {$in:[user_id]} },
{$group : {
_id : "$team_id"
}}
]).exec()
Upvotes: 1