MongoDB : - Merge object with same key into one - javascript

I am trying to merge an object inside an array with the same date but with a different key name for the status key.
I have 2 collections users and canteens
The query I am trying to get the result but am not able to figure out how to merge the object with the same Date
OUTPUT
User.aggregate([
{ $sort: { workerId: 1 } },
{
$lookup: {
from: "canteens",
localField: "_id",
foreignField: "employeeId",
pipeline: [
{
$match: {
Date: {
$gte: new Date(fromDate),
$lte: new Date(toDate),
},
},
},
{
$project: {
Date: 1,
status: 1,
},
},
],
as: "canteens",
},
},
{
$project: {
_id: 1,
workerId: 1,
workerFirstName: 1,
workerSurname: 1,
workerDepartment: 1,
workerDesignation: 1,
locationName: 1,
canteenData: "$canteens",
},
},
]);
[
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstaname",
"workerSurname": "lastname",
"workerId": "1",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status": "LUNCH",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b2db8db10c24487201e0a2",
"status": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status": "BREAK FAST",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b3d248c076184fb07ff2c4",
"status": "LUNCH",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b42b8ccb57a4cb7af34015",
"status": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
},
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstaname1",
"workerSurname": "lastname1",
"workerId": "2",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status": "LUNCH",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b2db8db10c24487201e0a2",
"status": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status": "BREAK FAST",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b3d248c076184fb07ff2c4",
"status": "LUNCH",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b42b8ccb57a4cb7af34015",
"status": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
}
]
The output I am trying to get
[
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstanem",
"workerSurname": "lastname",
"workerId": "1",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status1": "LUNCH",
"status2": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status1": "BREAK FAST",
"status2": "LUNCH",
"status3": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status1": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
},
{
"_id": "60e6fd3616dd663e84a925e2",
"workerFirstName": "Firstanem1",
"workerSurname": "lastname1",
"workerId": "2",
"locationName": "location",
"workerDesignation": "designation",
"workerDepartment": "department",
"canteenData": [
{
"_id": "63b285b9e92eee614feb7be1",
"status1": "LUNCH",
"status2": "DINNER",
"Date": "2023-01-02T00:00:00.000Z"
},
{
"_id": "63b39b247adbeb50bfbe3503",
"status1": "BREAK FAST",
"status2": "LUNCH",
"status3": "DINNER",
"Date": "2023-01-03T00:00:00.000Z"
},
{
"_id": "63b4ef71e038498fe6634506",
"status1": "BREAK FAST",
"Date": "2023-01-04T00:00:00.000Z"
}
]
}
]

One option is to add 2 steps into your $lookup pipeline aggregation:
{$group: {
_id: "$Date",
_idVal: {$first: "$_id"},
data: {$addToSet: "$status"}
}},
{$replaceRoot: {
newRoot: {
$mergeObjects: [
{_id: "$_idVal", Date: "$_id"},
{$arrayToObject: {
$reduce: {
input: "$data",
initialValue: [],
in: {$concatArrays: [
"$$value",
[{k: {$concat: [
"status",
{$toString: {$add: [{$size: "$$value"}, 1]}}
]},
v: "$$this"}]
]}
}
}}
]
}
}}
See how it works on the playground example

It's not easy to create status1, status2, ... variables dynamically + how do we know BREAK FAST should be status1 and not status2.
Alternative solution: We $group inside correlated subqueries and push all status values into an array
db.users.aggregate([
{
"$lookup": {
"from": "canteens",
"localField": "_id",
"foreignField": "employeeId",
pipeline: [
{
// Put your custom filters here
$match: {}
},
{
$group: {
_id: "$Date",
//pick "first" canteens _id
id: {
$first: "$_id"
},
status: {
$push: "$status"
}
}
},
{
$project: {
_id: "$id",
Date: "$_id",
status: 1
}
},
],
as: "canteenData",
}
}
])
MongoPlayground

Related

How to group result of lookup aggregate results

I aggregate users document:
User.aggregate([
{
$match: { _id: req.params.id },
},
{
$lookup: {
from: 'moods',
localField: '_id',
foreignField: 'source.userId',
pipeline: [
{
$lookup: {
from: 'contactrequests',
localField: 'source.userId',
foreignField: 'source.userId',
let: {
// truncate timestamp to start of day
moodsDate: {
$dateTrunc: {
date: '$timestamp',
unit: 'day',
},
},
},
pipeline: [
{
$match: {
$expr: {
$eq: [
'$$moodsDate',
{
// truncate timestamp to start of day
$dateTrunc: {
date: '$timestamp',
unit: 'day',
},
},
],
},
},
},
],
as: 'contactRequests',
},
},
],
as: 'calendar',
},
},
]).exec()
There is final document what I get
{
"_id": "P4SpYVd1KjBaF4SKyVw0E",
"lastName": "Doe",
"login": "User-01",
"name": "John"
"calendar": [
{
"_id": "62a351e33859aaf975c63323",
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
"deviceId": "Pacjent-141214"
},
"timestamp": "2022-06-07T12:44:13.333Z",
"mood": "good",
"contactRequests": []
},
{
"_id": "62a351f43859aaf975c63327",
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
"deviceId": "Pacjent-141214"
},
"timestamp": "2022-06-09T12:44:13.333Z",
"mood": "middle",
"contactRequests": [
{
"timestamp": "2022-06-09T12:44:13.333Z",
"source": {
"deviceId": "Pacjent-141214",
"userId": "P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": false,
"_id": "62a351ff3859aaf975c63329",
},
]
}
]
},
This is what I would to get. This is more clean and readable.
{
"_id": "P4SpYVd1KjBaF4SKyVw0E",
"login": "User-01",
"name": "John",
"lastName": "Doe",
"calendar": [
{
"timestamp": "2022-06-11T12:44:13.333Z"
"mood": {
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
},
"timestamp": "2022-06-11T12:44:13.333Z",
"mood": "bad",
"_id": "62a352b83859aaf975c6332d",
},
"contactRequest": [
{
"timestamp": "2022-06-11T15:25:13.333Z",
"source" : {
"userId":"P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": true,
"_id": "62a351ff3859aaf975c63329"
},
{
"timestamp": "2022-06-11T18:23:13.333Z",
"source" : {
"userId":"P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": false,
"_id": "62a351ff3859aaf975c63329"
},
]
}
}
]
}
To achive that I've used $group parameter, but at some point I have to declare which field should be fetch to result document and I have problem with contatRequest fields.
{
$group: {
_id: {
$dateToString: {
format: '%Y-%m-%d',
date: '$timestamp',
},
},
mood: {
$push: {
_id: '$_id',
source: '$source',
type: '$mood',
timestamp: '$timestamp',
},
},
contactRequest: {
$push: {
_id: '$contactRequest._id',
source: '$contactRequest.source',
resolve: '$contactRequest.resolve',
timestamp: '$contactRequest.timestamp',
},
},
},
},
{
$project: {
_id: 0,
timestamp: '$_id',
mood: 1,
contactRequest: 1,
},
},
Sample database/collections/aggregation pipeline at mongoplayground.net.

MongoDB query to get top selling products

I have a MongoDB collection "orders" with document similar to such:
{
"_id": ObjectId("xxxxx"),
"orderNo": 0,
"products": [
{ "product1": ["_Id": objectId("eeee"),"productName", "quantity", "price"] },
{ "product2": ["_Id": objectId("ffff"),"productName", "quantity", "price"] }
],
"deliveryAddress": "addsfdfghf"
}
{
"_id": ObjectId("yyyyy"),
"orderNo": 1,
"products": [
{ "product1": ["_Id": objectId("dddd"),"productName", "quantity", "price"] }
],
"deliveryAddress": "rwetrutytyi"
}
{
"_id": ObjectId("zzzz"),
"orderNo": 2,
"products": [
{ "product1": ["_Id": objectId("aaaa"), "productName", "quantity", "price"] },
{ "product2": ["_Id": objectId("bbbb"),"productName", "quantity", "price"] },
{ "product3": ["_Id": objectId("cccc"),"productName", "quantity", "price"] }
],
"deliveryAddress": "rwetrutytyi"
}
Now I want to write a query to get top selling products using "quantity" property of every products.
correct values of in above document look like:
{
"products": [{
"_id": {
"$oid": "612baf1b89c77b25c40aa3bc"
},
"productID": "NPZ6I",
"productName": "Cosentys 150ml",
"quantity": "1",
"expireDate": "04/20/2022",
"price": "22200"
}, {
"_id": {
"$oid": "612baf1b89c77b25c40aa3bb"
},
"productID": "NPZ3I",
"productName": "Asunra 400mg",
"quantity": "2",
"expireDate": "09/26/2021",
"price": "13200"
}],
"orderNo": 9,
"deliveryAddress": "Dhaka"
}
I want to return an object look like:
{
"_id": ObjectId("yyyyy"),
"productName": "String",
"productID": "String",
"totalSales" : "Number(sum of each products quantity)",
"totalEarning": "Number(sum of each product's price)"
}

How to compare two arrays and get matching output?

In my collection I have a category array as below.
I receive another array to my API like below
array = ['Chess','Rugby'];
I want to add a condition to my database query such that catName field from category objects exists in array.
currently I'm using the below code to get the results:
postSchemaModel.aggregate([{
"$geoNear": {
"near": { "type": "Point", "coordinates": [parseFloat(long), parseFloat(lat), ] },
"distanceField": "dist.calculated",
"maxDistance": parseInt(maxDistance),
"includeLocs": "dist.location",
"spherical": true
}
},
{ "$match": { "$or": [{ "typology": "post" }, { "typology": "chat_group" }] } },
{
"$match": {
"createdAt": {
"$gte": '2020-07-15 23:54:38.673665',
"$lt": '2020-06-15 23:54:38.673665'
}
}
},
{ "$limit": limit },
{ "$skip": startIndex },
{ "$sort": { "createdAt": -1 } },
{
"$lookup": {
"from": userSchemaModel.collection.name,
"localField": "user_id",
"foreignField": "_id",
"as": "user_id"
}
},
{
"$project": {
"post_data": 1,
"likes": 1,
"commentsCount": 1,
"post_img": 1,
"isUserLiked": 1,
"usersLiked": 1,
'exp_date': 1,
"has_img": 1,
"user_id": {
"img": "$user_id.img",
"_id": "$user_id._id",
"user_name": "$user_id.user_name",
"bday": "$user_id.bday",
"imagesource": "$user_id.imagesource",
"fb_url": "$user_id.fb_url",
},
"typology": 1,
"geometry": 1,
"category": 1,
"created": 1,
"createdAt": 1,
"updatedAt": 1,
}
},
]).then(async function(posts) {
//some code here
}
});
UPDATE : Sample Output
{
"_id": "5f0bd1b7d6ed4f0017e5177c",
"post_data": "bitch boy sudesh",
"likes": 2,
"commentsCount": 1,
"post_img": null,
"isUserLiked": true,
"usersLiked": [
"5f0bfa296ee76f0017f13787",
"5ef60bba10e9090017e2c935"
],
"exp_date": "2020-07-16T00:00:00.000Z",
"has_img": false,
"user_id": [
{
"img": [
"default-user-profile-image.png"
],
"_id": [
"5ef9a7a2922eba0017ce47e0"
],
"user_name": [
"Sudesh"
],
"bday": [
"1997-05-02T00:00:00.000Z"
],
"imagesource": [
"fb"
],
"fb_url": [
"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1846836948784193&width=400&ext=1596011605&hash=AeRsB0QJQH7edpRT"
]
}
],
"typology": "post",
"geometry": {
"pintype": "Point",
"_id": "5f0bd1b7d6ed4f0017e5177d",
"coordinates": [
79.9200017,
6.7088167
]
},
"category": [
{
"_id": "5f0bd1b7d6ed4f0017e5177e",
"catID": "5eef80cc5de48230887f3aa8",
"catName": "Chess"
},
{
"_id": "5f0bd1b7d6ed4f0017e5177e",
"catID": "5eef80cc5de48230887f3aa8",
"catName": "Rugby"
}
],
"created": 1594610103626,
"createdAt": "2020-07-13T03:15:03.629Z",
"updatedAt": "2020-07-18T14:02:35.080Z"
}
You can use some method if you only want to get true/false result:
category.some(element => array.includes(element.catName))
If you want to get an array of all the category objects with cat names that also exist in the array then you can filter method:
category.filter(element => array.includes(element.catName))
If you have an object called array in your code and you want to find at array of categories where cat names are in the array then you can add the condition to your $match stage:
{ "$match": { "$or": [{ "typology": "post" }, { "typology": "chat_group" }] }, "category.catName": { $in: array } }
Using another $match with "$elemMatch" solved the problem
"$match": {
"category": { "$elemMatch": { "catName": "Rugby", "catName": "Carrom" } },
}

Wrong Result by $lookup mongodb

I am using $lookup to get the data by joining data from two or three collections, Below is my aggregate query.
let condition = {status:{$ne:config.PROJECT_STATUS.completed}, assignId:mongoose.Types.ObjectId(req.params.id)};
Project.aggregate([
{
"$match": condition
},
{
"$group": { "_id": "$_id" }
},
{
"$lookup": {
"from": "worksheets",
"let": { "projectId": "$_id" },
"pipeline": [
{
"$match": { "$expr": { "$eq": ["$projectId", "$$projectId"] } }
},
{
"$group": { "_id": "$projectId", "totalHours": { "$sum": "$hours" } }
},
{
"$lookup": {
"from": "projects",
"let": { "projectId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$_id", "$$projectId"] } } },
{
"$lookup": {
"from": "users",
"let": { "developers": "$developers" },
"pipeline": [
{ "$match": { "$expr": { "$in": ["$_id", "$$developers"] } } },
{ "$project":{"firstName":1,"lastName":1}}
],
"as": "developers"
}
},
{
"$lookup": {
"from": "billing_accounts",
"let": { "upworkId": "$upworkId" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$_id", "$$upworkId"] } } },
{"$project":{"name":1,"username":1}}
],
"as": "upworkId"
}
},
{
"$project": {
"projectName": 1, "upworkId": 1, "status": 1, "developers": 1, "hoursApproved": 1
}
}
],
"as": "project"
}}
],
"as": "projects"
}
}
])
And it is giving me the below result:
[
{
"_id": "5c188a9959f6cf1258f4cb01",
"projects": [
{
"_id": "5c188a9959f6cf1258f4cb01",
"totalHours": 8,
"project": [
{
"_id": "5c188a9959f6cf1258f4cb01",
"hoursApproved": 192,
"developers": [
{
"_id": "5c0a29e597e71a0d28b910aa",
"lastName": "kumar",
"firstName": "Amit"
}
],
"projectName": "Jims fitness",
"status": "ongoing",
"upworkId": [
{
"_id": "5c17a1cec1a7681f7c54bb2d",
"name": "Heena Ln",
"username": "heena_ln"
}
]
}
]
}
]
},
{
"_id": "5c17a253c1a7681f7c54bb2f",
"projects": []
}
]
But what i want to get is:
[
{
"_id": "5c188a9959f6cf1258f4cb01",
"projects": [
{
"_id": "5c188a9959f6cf1258f4cb01",
"totalHours": 0,
"project": [
{
"_id": "5c188a9959f6cf1258f4cb01",
"hoursApproved": 192,
"developers": [
{
"_id": "5c0a29e597e71a0d28b910aa",
"lastName": "kumar",
"firstName": "Amit"
}
],
"projectName": "Project1",
"status": "ongoing",
"upworkId": [
{
"_id": "5c17a1cec1a7681f7c54bb2d",
"name": "Heena Ln",
"username": "heena_ln"
}
]
}
]
}
]
},
{
"_id": "5c17a253c1a7681f7c54bb2f",
"projects": [
{
"_id": "5c17a253c1a7681f7c54bb2f",
"totalHours": 0,
"project": [
{
"_id": "5c17a253c1a7681f7c54bb2f",
"hoursApproved": 192,
"developers": [
{
"_id": "5c0a29e597e71a0d28b910a9",
"lastName": "kumar",
"firstName": "Rajat"
}
],
"projectName": "project2",
"status": "ongoing",
"upworkId": [
{
"_id": "5c17a1cec1a7681f7c54bb2d",
"name": "Heena Ln",
"username": "heena_ln"
}
]
}
]
}
]
}
]
As you can see that now i have totalHours equals to 0 instead of empty array and have the project details.
Actually I have four collections: projects, worksheets, users and billings and i am executing aggregate query on the projects collection to get the projects of a project manager and for this i am also joining worksheets collection to get the data for how many hours the employees worked on this project, because worksheets collection contains the projectId, userId and hours.
Query: You can see in the result that, i am getting the empty array of projects, this is because i don't have any record of second project projectId into the worksheet collection, so for this it is giving me empty array, but i want to get the projects details as it is and totalHours equals to 0.

How to find average in mongoose aggregation

I am aggregating data from 3 collections -
Users -
[
{
"_id": "5bc425bb3e99c8269ce51faa",
"fname": "Akash",
"salary": 25000,
"age": 23,
"__v": 0
},
{
"_id": "5bc425e23e99c8269ce51fab",
"fname": "Aditya",
"salary": 20000,
"age": 28,
"__v": 0
}
]
Balance -
[
{
"_id": "5bc4349adcc885e047054466",
"fname": "Akash",
"balance": 4000
},
{
"_id": "5bc434ffdcc885e0470544c2",
"fname": "Aditya",
"balance": 8000
}
]
dept collection -
[
{
"_id": "5bc46167b365918881aa3bbf",
"fname": "Akash",
"dept": "IT"
},
{
"_id": "5bc4620fb365918881aa3c1b",
"fname": "Apurva",
"dept": "Chemical"
}
]
I want to aggregriated result as -
[
{
"_id": {
"fname": "Aditya",
"balance": 8000,
"dept": "Medical",
"salary": 20000
},
"avg": 20000
},
{
"_id": {
"fname": "Apurva",
"balance": 1000,
"dept": "Chemical",
"salary": 22000
},
"avg": 22000
}
]
My query -
User.aggregate([
{
$lookup: {
from: "balance",
localField: "fname",
foreignField: "fname",
as: "result"
}
},
{
$unwind: "$result"
},
{
$lookup: {
from: "dept",
localField: "fname",
foreignField: "fname",
as: "deptresult"
}
},
{
$unwind: "$deptresult"
},
{ $sort: { salary: -1 } },
// { $limit: 1 },
{
$project: {
fname: 1,
salary: 1,
"result.balance": 1,
"deptresult.dept": 1,
avg: { $avg: "$salary" }
}
}
It shows remaining result but it does now display calculated average.
Instead of average , it simply prints salary . Please someone guide me on this.
I am using mongoose ORM.Rest of the code is running fine.
You can use below aggregation
User.aggregate([
{ "$facet": {
"projection": [
{ "$lookup": {
"from": "balance",
"localField": "fname",
"foreignField": "fname",
"as": "result"
}},
{ "$lookup": {
"from": "dept",
"localField": "fname",
"foreignField": "fname",
"as": "deptresult"
}},
{ "$unwind": "$deptresult" },
{ "$unwind": "$result" },
{ "$sort": { salary: -1 } },
{ "$project": {
"fname": 1,
"salary": 1,
"result.balance": 1,
"deptresult.dept": 1
}}
],
"averageSalary": [
{ "$group": {
"_id": null,
"avg": { "$avg": "$salary" }
}}
]
}}
])

Categories

Resources