Multiple arguments in mongodb aggregate - angularjs

I'm trying to get multiple arguments in my mongodb, My code here gets the distinct values for date and count it, but how can I add another argument to get for example another set of distinct and count.
Example:
Here is the output of my code:
[ { _id: '05-09-2019', count3: 1 },
{ _id: '06-09-2019', count3: 1 },
{ _id: '06-21-2019', count3: 1 },
{ _id: '06-30-2019', count3: 5 },]
How can I achieve this:
[ { _id: '05-09-2019', count: 1, branch: America, count2: 1},
{ _id: '06-09-2019', count: 1, branch: Germany,count2: 1},
{ _id: '06-21-2019', count: 1, branch: Philippines,count2: 1},
{ _id: '06-30-2019', count: 5 , branch: Vietnam,count2: 1},]
Here is my code to get the distinct values of date while counting it:
router.get('/blooddonationarea', function(req, res) {
Blooddonation.aggregate([{$group: {_id : "$date" , count :{$sum:1}}},{$sort: {_id: 1}}],function(err, date) {
res.json({ success: true, date: date });
//console.log(date);
});
});
I tried inserting this after the [] clause but it doesn't get the values at all
[{ $match: { branch: { $eq: 'Rizal' } } },{$group: {_id : "$date" , count2 :{$sum:1}}},{$sort: {_id: 1}}]
Here is the sample document:
_id:5c1f47dd59cdd931f4ce98ae
blood_product_donated:"Whole Blood"
branch:"Rizal"
deferral_type:"permanent"
date:"10-22-2019"

You could start with grouping the documents by both the date and branch keys, then a further pipeline step where you only group by the
date key on the documents from the previous pipeline. Following this example will give you the desired result:
router.get('/blooddonationarea', (req, res) => {
Blooddonation.aggregate([
{ '$group': {
'_id': { 'date': '$date', 'branch': '$branch' },
'count': { '$sum': 1 }
} },
{ '$group': {
'_id': '$_id.date',
'count': { '$sum': '$count' },
'branch': { '$first': '$_id.branch' },
'count2': { '$first': '$count' }
} },
{ '$sort': { '_id': 1 } }
], (err, date) => {
console.log(date);
if (err) throw err
res.json({ success: true, date });
});
});

Related

MongoDB: nested array count + original document

I have the following document structure which contains an array of votes:
{ _id: ObjectId("6350e2c1a15e0e656f4a7472"),
category: 'business',
votes:
[ { voteType: 'like',
userId: ObjectId("62314007da34df3f32f7cfc0") },
{ voteType: 'like',
userId: ObjectId("6356b5cbe2272ebf628451b") } ] }
What I would like to achieve is to add for each document the sum of votes for which voteType = like, while keeping the original document, such as:
[ [{ _id: ObjectId("6350e2c1a15e0e656f4a7472"),
category: 'business',
votes:
[ { voteType: 'like',
userId: ObjectId("62314007da34df3f32f7cfc0") },
{ voteType: 'like',
userId: ObjectId("6356b5cbe2272ebf628451b") } ] }, {sum: 2, voteType: "like"} ], ...]
At the moment, the only workaround that I found is through an aggregation although I cannot manage to keep the original documents in the results:
db.getCollection('MyDocument') .aggregate([ {
$unwind: "$votes" }, {
$match: {
"votes.voteType": "like",
} }, {
$group: {
_id: {
name: "$_id",
type: "$votes.voteType"
},
count: {
$sum: 1
}
} },
{ $sort : { "count" : -1 } }, {$limit : 5}
])
which gives me:
{ _id: { name: ObjectId("635004f1b96e494947caaa5e"), type: 'like' },
count: 3 }
{ _id: { name: ObjectId("63500456b96e494947cbd448"), type: 'like' },
count: 3 }
{ _id: { name: ObjectId("63500353b6c7eb0a01df268e"), type: 'like' },
count: 2 }
{ _id: { name: ObjectId("634e315bb7d17339f8077c39"), type: 'like' },
count: 1 }
You can do it like this:
$cond with $isArray - to check if the votes property is of the type array.
$filter - to filter votes based on voteType property.
$size - to get the sized of the filtered array.
db.collection.aggregate([
{
"$set": {
"count": {
"$cond": {
"if": {
"$isArray": "$votes"
},
"then": {
"$size": {
"$filter": {
"input": "$votes",
"cond": {
"$eq": [
"$$this.voteType",
"like"
]
}
}
}
},
"else": 0
}
}
}
}
])
Working example

Mongo aggregation framework match a given _id

My model :
const scheduleTaskSchema = new Schema({
activity: { type: Object, required: true },
date: { type: Date, required: true },
crew: Object,
vehicle: Object,
pickups: Array,
details: String,
});
const ScheduleTaskModel = mongoose.model("schedule_task", scheduleTaskSchema),
and this aggregation pipeline :
let aggregation = [
{
$sort: {
"pickups.0.time": 1,
},
},
{
$group: {
_id: "$date",
tasks: { $push: "$$ROOT" },
},
},
{ $sort: { _id: -1 } },
];
if (hasDateQuery) {
aggregation.unshift({
$match: {
date: { $gte: new Date(start_date), $lte: new Date(end_date) },
},
});
} else {
aggregation.push({ $limit: 2 });
}
const scheduledTasksGroups = await ScheduleTaskModel.aggregate(aggregation);
the crew object can have arbitrary number of keys with this structure :
crew : {
drivers: [
{
_id: "656b1e9cf5b894a4f2v643bc",
name: "john"
},
{
_id: "567b1e9cf5b954a4f2c643bhh",
name: "bill"
}
],
officers: [
{
_id: "655b1e9cf5b6632a4f2c643jk",
name: "mark"
},
{
_id: "876b1e9af5b664a4f2c234bb",
name: "jane"
}
],
//...any number of keys that contain an array of objects that all have an _id
}
I'm looking for a way to return all documents (before sorting/grouping) that contain a given _id anywhere within the crew object without knowing which key to search,it can be many different keys that all contain an array of objects that all have an _id
Any ideas ?
You can use $objectToArray for this:
db.collection.aggregate([
{$addFields: {crewFilter: {$objectToArray: "$crew"}}},
{$set: {
crewFilter: {$size: {
$reduce: {
input: "$crewFilter",
initialValue: [],
in: {$concatArrays: [
"$$value",
{$filter: {
input: "$$this.v",
as: "member",
cond: {$eq: ["$$member._id", _id]}
}
}
]
}
}
}}
}},
{$match: {crewFilter: {$gt: 0}}}
])
See how it works on the playground example

MongoDB using skip and distinct in a query based on values inside an array

So I have document that is structure like this
_id: ObjectId('62bbe17d8fececa06b91873d')
clubName: 'test'
staff:[
'62bbe47f8fececa06b9187d8'
'624f4b56ab4f5170570cdba3' //IDS of staff members
]
A single staff can be assigned to multiple clubs so what I'm trying to achieve is to get all staff that has been assigned to at least one club and display them on a table on the front end, I followed this solution since distinct and skip can't be used on a single query but it just returned this:
[
{ _id: [ '624f5054ab4f5170570cdd16', '624f5054ab4f5170570cdd16' ] } //staff from club 1,
{ _id: [ '624f5054ab4f5170570cdd16', '624f9194ab4f5170570cded1' ] } //staff from club 2,
{ _id: [ '624f4b56ab4f5170570cdba3' ]} //staff from club 3
]
my desired outcome would be like this:
[ _id : ['624f5054ab4f5170570cdd16', '624f9194ab4f5170570cded1', '624f4b56ab4f5170570cdba3'] ]
here's my query:
const query = this.clubModel.aggregate(
[{ $group: { _id: '$staff' } }, { $skip: 0}, { $limit: 10}],
(err, results) => {
console.log(results);
},
);
the values returned are not distinct at all, is there an operation that can evaluate the value inside an array and make them distinct?
Here's my new query after adding the 'createdAt' field in my document structure:
const query = this.clubModel.aggregate([
{ $sort: { createdAt: -1 } },
{
$unwind: '$drivers',
},
{
$project: {
isActive: true,
},
},
{
$group: {
_id: 'null',
ids: {
$addToSet: '$drivers',
},
},
},
{
$project: {
_id: 0,
},
},
{
$skip: skip,
},
{
$limit: limit,
},
]);
Does this works for you, first UNWIND the staff array, and then group on "_id" as null and add staff values using $addToSet:
db.collection.aggregate([
{
"$unwind": "$staff"
},
{
"$group": {
"_id": "null",
"ids": {
"$addToSet": "$staff"
}
}
},
{
"$project": {
"_id": 0,
}
},
{
$skip: 0
},
{
$limit: 10
}
])
Here's the working link.

How to query a date that have 20 or more documents in mongoDB in the same day and hour

I have a mongoDB database structure like this
{
_id: ObjectId,
name: string,
scheduledDate: ISOString
}
I want to return all scheduledDates that repeat the same scheduledDate day 2 times or more across all the database
Example:
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-15T05:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-15T07:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-18T02:20:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-10T05:44:00.000Z"
}
In this example 2022-04-15 repeats 2 times and 2022-04-18 repeat 3 times, so both match the criteria (2 times or more) so I want to return both date day
Is this possible?
Like this:
{
scheduledDate:"2022-04-15T00:00:00.000Z"
},
{
scheduledDate:"2022-04-18T00:00:00.000Z"
}
And one more question, is possible to do the same with hours? A list of specific hours of scheduledDate that repeat across all database X times
Use $group with $date and $dateTrunc
db.collection.aggregate([
{
$group: {
_id: {
$dateTrunc: {
date: { $toDate: "$scheduledDate" },
unit: "day"
}
},
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
},
{
$project: {
_id: 0,
scheduledDate: "$_id"
}
}
])
mongoplayground

mongoose push nested subdocument array

i need to push data in nested subdocument array:
this is my Json:
[
{
_id: "56cbe9727bab33a413216dd4",
company_name: "Star",
__v: 0,
created_at: "2016-02-23T05:09:06.754Z",
plan: [
{
plan_name: "Mediclassic",
plan_type: "Individual",
_id: "56cbe9727bab33a413216dd5",
created_at: "2016-02-23T05:09:06.756Z",
rate_card: [
{
zone: "zone-2",
suminsured: 150000,
premium: 2874,
size: "1A",
_id: "56cbe9727bab33a413216dd6",
benefits: {
hospitals: true,
copay: true,
cashless_hospital: true,
existing_disease_cover: true
},
age: {
min: "5M",
max: 35
}
}
]
}
]
}
]
I need to push my data in rate_card, i have tried something but its not working
below is my code:
AngularController.js
$scope.RateCardCreateAction = function(){
$scope.params = $routeParams.id;
console.log($scope.insurance.plan.rate_card);
$http.put('/auth/insurance-list/plan/new/rate_card/' + $scope.params, $scope.insurance.plan.rate_card).success(function(response){
console.log(response);
refresh();
});
};
mainRoutes.js
// New Rate Card
router.put('/secure/insurance-list/plan/new/rate_card/:id', function(req, res){
var id = req.params.id;
Insurance.update({'plan._id':id}, {$push :
{
'rate_card' : {
'zone' : req.body.zone,
'suminsured' : req.body.suminsured,
'premium' : req.body.premium,
'size' : req.body.size,
'age' : {
'min' : req.body.age.min,
'max' : req.body.age.max
},
'benefits' : {
'hospitals' : req.body.benefits.hospitals,
'cashless_hospital' : req.body.benefits.cashless_hospital,
'copay' : req.body.benefits.copay,
'existing_disease_cover' : req.body.benefits.existing_disease_cover
}
}
}
}, {upsert: true}, function(err, docs){
res.json(docs);
console.log(docs);
});
});
Here i need to push data to rate_card, but data is not storing in subdocument array, here am getting plan id and trying to push but is not working
in server response am getting this { ok: 0, n: 0, nModified: 0 }
Try this, i hope this will help you
router.put('/secure/insurance-list/plan/new/rate_card/:id/:id2', function(req, res){
var id = req.params.id;
var id2 = req.params.id2;
Insurance.update({_id:id, 'plan._id':id2}, {$push :
{'plan.$.rate_card' : req.body}
}, {upsert: true}, function(err, docs){
res.json(docs);
console.log(docs);
});
});
If somehow your inserted value is not present then it would be great to use $addToSet.
var updateArr = {
'roomUsers':{
userId:req.session.userInfo._id,
requestDate:Date.now(),
status:1
}
};
roomInfo.findOneAndUpdate({_id:roomId}, {$addToSet:updateArr}, (err, result) => {
if (!err && result) {
res.send({
status:true
});
}
else{
res.send({
status:false
});
}
});
I got it done using below update query:
db.insurance.updateOne(
{
_id: ObjectId('60659595afcb710627e3fad7')
},
{
$push: {
"plan.$[element].rate_card": {
$each: [
{ _id: ObjectId('6062bd90c5a1d66fdd2c91bc'), premium: 2874, size: "1A" },
{ _id: ObjectId('6062bd90c5a1d66fdd2c91bc'), premium: 2874, size: "1A" }]
}
}
},
{
arrayFilters: [{ "element._id": ObjectId('6065a35ee1e5f1153504e861') }]
}
);
In $each you need to add your array of object.
element._id is your subdocument id (above case is plan _id).

Resources