How to count rating on group aggregation? - node.js

I was stuck on mongodb aggregation to get count of rating. i have already trying to make myself pipeline. looks above
data (products collection)
{
"status": 200,
"message": null,
"data": {
"_id": "5cc570257631a313d489ba4a",
"media": [
"httpsdssd",
"dfdfd"
],
"comment": [
"5cc57f1053273c05cc60e707",
"5cc585bf6ff7a812e0e7d9d9",
"5cc5c654bc73b408787ffadc",
"5cc5c6e3bc73b408787ffadd"
],
"store": "5cc2c9710bc5d615781fcf8a",
"meta": {
"title": "Traveling Sumbar",
"price": "150000",
"max": 5,
"duration": 6,
"description": "fdf fdnf jdnf dfnkdknfkkd",
"location": {
"province": "Sumbar",
"city": "Padang"
}
},
"option": {
"is_promo": false,
"auto_delete": null
},
"created_at": "2019-04-28T09:19:33.233Z",
"updated_at": "2019-04-28T15:29:39.921Z",
"__v": 0
}
}
comment data on (products_comment)
{
"helped": [],
"deleted_at": null,
"_id": "5cc3276e32940613506c3848",
"user": "5cc2c7fb0bc5d615781fcf86",
"rating": "4",
"body": "fdfdlfdlfkdlfkdlfkd",
"created_at": "2019-04-26T15:44:46.224Z",
"updated_at": "2019-04-28T16:00:48.400Z",
"__v": 0
},
{
"helped": [],
"deleted_at": null,
"_id": "5cc3276e32940613506c3848",
"user": "5cc2c7fb0bc5d615781fcf86",
"rating": "4",
"body": "fdfdlfdlfkdlfkdlfkd",
"created_at": "2019-04-26T15:44:46.224Z",
"updated_at": "2019-04-28T16:00:48.400Z",
"__v": 0
},
{
"helped": [],
"deleted_at": null,
"_id": "5cc3276e32940613506c3848",
"user": "5cc2c7fb0bc5d615781fcf86",
"rating": "3",
"body": "fdfdlfdlfkdlfkdlfkd",
"created_at": "2019-04-26T15:44:46.224Z",
"updated_at": "2019-04-28T16:00:48.400Z",
"__v": 0
},
I have already try make aggregation pipeline like this
{
$lookup: {
from: "stores",
localField: "store",
foreignField: "_id",
as: "store"
}
},
{
$lookup: {
from: "products_comment",
localField: "comment",
foreignField: "_id",
as: "comment"
}
},
{ $unwind: "$comment" },
{
$project: {
media: 1,
"store.type": 1,
"store.profile.address.city": 1,
"meta.title": 1,
"meta.price": 1,
"comment.rating": 1
}
}
but result unlike expectation, i want result like this
{
"_id": "5cc570257631a313d489ba4a",
"media": [
"httpsdssd",
"dfdfd"
],
"comment": {
1_rating: 0, <value of rating: count of value>
2_rating: 3,
3_rating: 5,
....,
},
"store": [
{
"type": "craft",
"profile": {
"address": {
city: "Padang
}
}
}
],
"meta": {
"title": "Traveling Sumbar",
"price": "150000"
}
}
how i do to solve my problem ?

Below Query will give you exactly expected Output :
var query = [
{
$lookup: {
from: "comments",
localField: "comment",
foreignField: "_id",
as: "comments"
}
},
{ $unwind: "$comments" },
{ $group : {
_id: {
_id: '$_id',
rating: '$comments.rating',
media : '$media',
meta : '$meta',
store : '$store'
},
totalRating: {$sum: 1}
}
},
{
$group : {
_id : {
_id : '$_id._id',
media : '$_id.media',
meta : '$_id.meta',
store : '$_id.store'
},
comments : {
$push : {
rating : '$_id.rating',
totalRating : '$totalRating'
}
}
}
},
{
$lookup: {
from: "stores",
localField: "store",
foreignField: "_id",
as: "store"
}
},
{
$project: {
'_id' : '$_id._id',
media : '$_id.media',
store : '$store',
meta : {
title: '$_id.meta.title',
price : '$_id.meta.price'
},
comments : { "$arrayToObject": {
"$map": {
"input": "$comments",
"as": "el",
"in": {
"k": "$$el.rating",
"v": "$$el.totalRating"
}
}
}
}
}
}
];
Output :
{
"_id" : ObjectId("5cc718715290f4ed550f5305"),
"media" : [
"httpsdssd",
"dfdfd"
],
"store" : [ ],
"meta" : {
"title" : "Traveling Sumbar",
"price" : "150000"
},
"comments" : {
"3" : 1,
"4" : 2
}
}
{
"_id" : ObjectId("5cc88d99d486568c5745e4b7"),
"media" : [
"maha",
"sagar"
],
"store" : [ ],
"meta" : {
"title" : "Sagar Sumbar",
"price" : "15000"
},
"comments" : {
"3" : 2,
"5" : 1,
"1" : 1
}
}
NOTE: Store data will be fetched from stores collection by $lookup. I don't have a model/data so, not in the output.

Related

How to make a nested relationship on mongodb?

For example i have 4 collections. There are:
"company" collection
{
"id_company": "C01"
"company_name": "Sidomuncul"
"like": [
"123",
"121"
]
}
"user" collection
{
"id_user": "123",
"name": "Astra",
"major": "111",
"language": [{
"id_language": "101",
"level": "Expert"
}]
},
{
"id_user": "121",
"name": "Bibi",
"id_major": "112",
"language": [{
"id_language": "102",
"level": "Intermediate"
}]
}
"major" collection
{
"id_major": "111",
"name": "IT"
},
{
"id_major": "112",
"name": "Designer"
}
"language" collection
{
"id_language": "101",
"name": "English"
},
{
"id_language": "102",
"name": "Chinese"
}
And when i make a route for get who are like company by id_company "C01", i want show the result relation id_user in "like" field with user collection. Example result:
{
"id_company": C01",
"like": [
{
"id_user": "123",
"name": "Astra",
"major": "IT",
"language": [{
"id_language": "English",
"level": "Expert"
}]
},
{
"id_user": "121",
"name": "Bibi",
"id_major": "Designer",
"language": [{
"id_language": "Chinese",
"level": "Intermediate"
}]
}
] //Close Like Field
}
Thanks before.
SOLVED
Use lookup and group also push
Using Mongo aggregate pipeline you can collect required result in multiple stages. For mongo aggregate query you can use $lookup, $set, $unwind and $group stages. Try this query, you might get required result set:
db.company.aggregate([
{
"$unwind": "$like"
},
{
$lookup: {
from: "user",
localField: "like",
foreignField: "id_user",
as: "like"
}
},
{
"$unwind": "$like"
},
{
$lookup: {
from: "major",
localField: "like.major",
foreignField: "id_major",
as: "major"
}
},
{
"$unwind": "$major"
},
{
$set: {
"like.major": "$major.name"
}
},
{
$lookup: {
from: "language",
localField: "like.language.id_language",
foreignField: "id_language",
as: "lang"
}
},
{
"$unwind": "$lang"
},
{
$set: {
"like.language.id_language": "$lang.name"
}
},
{
$group: {
_id: "id_company",
company_name: {
"$first": "$company_name"
},
like: {
$push: {
id_user: "$like.id_user",
major: "$like.major",
name: "$like.name",
language: "$like.language"
}
}
}
}
])

How to populate an ObjectID in group command in mongoDB?

I have collection named "report" like this:
{
"_id" : ObjectId("5fc51722d6827f3bfd24e3b0"),
"is_deleted" : false,
"reporter" : ObjectId("5fb7b85f516b9709af5c7bc2"),
"violator" : ObjectId("5fb8a07e9cd2840f5f6bac5a"),
"reportNote" : "vi pham",
"status" : 0,
"createdAt" : ISODate("2020-11-30T16:00:34.013Z"),
"updatedAt" : ISODate("2020-11-30T16:00:34.013Z"),
"__v" : 0
}
With "reporter" and "violator" is ObjectID that reference from "User" collection
Now I want to find a list of violator and re-oder it from larger to small, so I do like this.
db.report.aggregate([
{ $group: { _id: "$violator", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
And I have result as below.
{
"data": [
{
"_id": "5fb8a07e9cd2840f5f6bac5a",
"count": 10
},
{
"_id": "5fbcbe855e26df3af08ffcee",
"count": 7
},
{
"_id": "5fbcb990cb35042db064b2b0",
"count": 6
}
],
"total": 23,
"message": ""
}
My expected result is
{
"data": [
{
"_id": "5fb8a07e9cd2840f5f6bac5a",
"name": "David",
"email": "david#gmail.com",
"count": 10
},
{
"_id": "5fbcbe855e26df3af08ffcee",
"name": "Vincent",
"email": "Vincent#gmail.com",
"count": 7
},
{
"_id": "5fbcb990cb35042db064b2b0",
"name": "robert",
"email": "robert#gmail.com",
"count": 6
}
],
"total": 23,
"message": ""
}
I did follow turivishal recommend.
db.report.aggregate([
{ $group: { _id: "$violator", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{
$lookup:
{
from: "users",
localField: "violator",
foreignField: "_id",
as: "ViolatorDetail"
}
}
])
But the result of ViolatorDetail (User) is empty.
{
"data": [
{
"_id": {
"violator": "5fb8a07e9cd2840f5f6bac5a",
"status": 0,
"reportNote": "vi pham"
},
"count": 10,
"ViolatorDetail": []
},
{
"_id": {
"violator": "5fbcbe855e26df3af08ffcee",
"status": 0,
"reportNote": "vi pham"
},
"count": 7,
"ViolatorDetail": []
},
{
"_id": {
"violator": "5fbcb990cb35042db064b2b0",
"status": 0,
"reportNote": "vi pham"
},
"count": 6,
"ViolatorDetail": []
}
],
"total": 23,
"message": ""
}

How to change this response to simple array?

{
"success": true,
"message": "Result",
"data": [
{
"Here": [
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1#test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1",
}
]
},
{
"Here": [
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2#test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
}
],
}
What I am expecting is this
{
"success": true,
"message": "Result",
data: [
{
"_id": "5ee97ee7f25d1c1482717bdf",
"email": "test1#test.io",
"profileImages": [],
"username": "test1",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location",
"firstName": "test1",
"lastName": "test1"},
{
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2#test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
}
Query I am using is for this response is below using aggregation in mongodb, lookup and project which is leading me to the some undesired response
db.collections.aggregate( [
{
$lookup: {
from: 'users',
as: 'Here',
let: {
whoDid: '$whoDid'
},
pipeline: [
{
"$match": { "$expr": { "$eq": ["$_id", "$$whoDid"] } }
},
{
$project: {
_id: 1,
email: 1,
profileImages: 1,
username: 1,
birthday: 1,
phoneNumber: 1,
firstName: 1,
lastName: 1,
fullName: 1,
// age: {$year: "$birthday"}
age: {
$divide: [{ $subtract: [new Date(), "$birthday"] },
(31558464000)]
}
}
}
],
}
},
{
$project:{
Here:1,
_id:0
}
} ,
])
who did table is one of the collection I have where I have stored the user Id and later I am populating the data using lookup
{
"_id" : ObjectId("5ee988eb1aac0022e15dbb7b"),
"whoDid" : ObjectId("5ee97ef2f25d1c1482717be1"),
"toWhomDid" : ObjectId("5ee97ec0f25d1c1482717bdd"),
"modified_at" : ISODate("2020-06-17T03:07:23.217Z"),
"created_at" : ISODate("2020-06-17T03:07:23.217Z"),
"__v" : 0
}
{
"_id" : ObjectId("5ee988eb1aac0022e15dbb7c"),
"whoDid" : ObjectId("5ee97ec0f25d1c1482717bdd"),
"toWhomDid" : ObjectId("5ee97ef2f25d1c1482717be1"),
"modified_at" : ISODate("2020-06-17T03:07:23.220Z"),
"created_at" : ISODate("2020-06-17T03:07:23.220Z"),
"__v" : 0
}
Can anyone suggest me any better option so that I can get a desired respose?
It is possible to use reduce method:
obj.data = obj.data.reduce((a, c) => {
a.push(...c.Here);
return a;
}, [])
An example:
let obj = {
"success": true,
"message": "Result",
"data": [ {
"Here": [ {
"_id": "5ee97ee7f25d1c1482717bdf", "email": "test1#test.io",
"profileImages": [], "username": "test1",
"birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location",
"firstName": "test1", "lastName": "test1",
}
]
},
{
"Here": [ {
"_id": "5ee97ef2f25d1c1482717be1",
"email": "test2#test.io",
"profileImages": [],
"username": "test2",
"birthday": "2020-06-11T10:11:32.000Z",
"phoneNumber": "+910000000000",
"location": "Test Location"
}
]
}
]
};
obj.data = obj.data.reduce((a, c) => {
a.push(...c.Here);
return a;
}, [])
console.log(obj);
Add these extra steps into your aggregation pipeline:
{
$unwind: "$Here"
},
{
$replaceWith: "$Here"
}
MongoPlayground
Note: You can replace $project: { _id: 1, email: 1, ... to this:
{
$addFields:{
age: {
$divide: [{ $subtract: [new Date(), "$birthday"] },(31558464000)]
}
}
}

Issue with to get data from MongoDB aggregate

I want data from places where userid is equal to the body.user._id
PLACE DATA
{
"_id": "5e8ddd0f7f5b174290bbefc8",
"type": 0,
"location": [
24.8757396,
67.3464698
],
"title": "E 22, Steel Town Karachi, Karachi City, Sindh, Pakistan",
"googlePlaceId": "ChIJlbvA8BIzsz4Rlh7w_fKfwus",
"createdDate": "2020-04-08T14:17:51.869Z",
"__v": 0,
},
{
"_id": "5e8de4204396310017564a2b",
"type": 0,
"location": [
24.910688,
67.0310973
],
"title": "Nazimabad, Karachi, Pakistan",
"googlePlaceId": "ChIJzZudRr0_sz4R81KZ48Ylk3Q",
"createdDate": "2020-04-08T14:48:00.557Z",
"__v": 0,
},
{
"_id": "5e8de4364396310017564a2d",
"type": 0,
"location": [
24.9180271,
67.0970916
],
"title": "Gulshan-e-Iqbal, Karachi, Pakistan",
"googlePlaceId": "ChIJsda_CLg4sz4RIghXwgIae5k",
"createdDate": "2020-04-08T14:48:22.979Z",
"__v": 0,
},
{
"_id": "5e8dea79894854524cc554e0",
"type": 0,
"location": [
24.9343322,
67.177173
],
"title": "Malir Cantt Check Post No 6, Malir Link to Super Highway, Karachi, Pakistan",
"googlePlaceId": "ChIJJ7BbsyQ4sz4RvpkV9Ig_aU4",
"createdDate": "2020-04-08T15:15:05.360Z",
"__v": 0,
}**
Visited Places DATA
{
"_id":"5e90998f8bc84d0017a6d2f3",
"visitingNo":"0",
"_userId":"5e8f3ef5434f5800170c7169"
"_placeId":"5e908fdb8bc84d0017a6d2e8"
"createdDate":"2020-04-10T16:06:39.231+00:00"
"__v":"0"
},
{
"_id":"5e90998f8bc84d0017a6d2f3",
"visitingNo":"0",
"_userId":"5e8f3ef5434f5800170c7169"
"_placeId":"5e908fdb8bc84d0017a6d2e8"
"createdDate":"2020-04-10T16:06:39.231+00:00"
"__v":"0"
},
{
"_id":"5e90998f8bc84d0017a6d2f3",
"visitingNo":"0",
"_userId":"5e8f3ef5434f5800170c7169"
"_placeId":"5e908fdb8bc84d0017a6d2e8"
"createdDate":"2020-04-10T16:06:39.231+00:00"
"__v":"0"
},
MY CODE
const palace = placeData.aggregate([
{
$lookup:{
from: `${visitplaceData}`,
// localField: "_id",
// foreignField: "_placeId",
let : {
"placeId" : "$_id"
},
pipeline : [
{ $sort: { visitingNo : -1 } },
{
$match : {
$expr : {
$and : [
{$eq: [ "$_placeId", "$$placeId" ]},
{"_userId": body.user._id}
]
}
}
},
],
as: "places"
}
}
])

How to combine nested $lookup 3 level in mongoose?

I want to combine separate collections to 1 json, but I have some problems in nested $lookup.
This is my example collections.
Collection Package_subscriptions:
[
{
"_id": "1",
"package_name": "Small",
"package_desc": "AAA",
"package_price": 10
},
{
"_id": "2",
"package_name": "Medium",
"package_desc": "BBB",
"package_price": 20
}
Collection Package_modules:
{
"_id" : 1,
"subscription_id" : 1,
"billing_model_id" : 1,
"module_id" : 1,
"created_at" : ISODate("2019-06-17T07:59:43.199Z"),
}
{
"_id" : 2,
"subscription_id" : 1,
"billing_model_id" : 3,
"module_id" : 2,
"created_at" : ISODate("2019-06-17T08:00:37.464Z"),
}
{
"_id" : 3,
"subscription_id" : 2,
"billing_model_id" : 2,
"module_id" : 1,
"created_at" : ISODate("2019-06-17T08:00:56.610Z"),
}
{
"_id" : 4,
"subscription_id" : 2,
"billing_model_id" : 4,
"module_id" : 2,
"created_at" : ISODate("2019-06-17T08:01:29.667Z"),
}
Collection Modules:
{
"_id" : 1,
"module_name" : "Call",
"status" : "Active",
}
{
"_id" : 2,
"module_name" : "SMS",
"status" : "Active",
}
Collection Billing_models
{
"_id" : 1,
"unit_count" : "Menit",
"counter" : 2000,
},
{
"_id" : 2,
"unit_count" : "Menit",
"counter" : 3000,
}
{
"_id" : 3,
"unit_count" : "SMS",
"counter" : 3000,
},
{
"_id" : 4,
"unit_count" : "SMS",
"counter" : 5000,
}
This is my code to try the issue, but there's not as expected.
Package_subscription
.aggregate([
{
$lookup: {
from: "o_package_modules",
localField: "_id",
foreignField: "subscription_id",
as: "package_modules"
}
},
{
$unwind: {
path: "$package_modules",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "o_modules",
localField: "package_modules.module_id",
foreignField: "_id",
as: 'module'
}
},
{
$lookup: {
from: "o_billing_models",
localField: "package_modules.billing_model_id",
foreignField: "_id",
as: 'billing_module'
}
}
])
.exec()
.then((pricing) => {
res.json(pricing)
})
.catch((err) => {
res.send(err)
})
I expect the output is:
[
{
"_id": "1",
"package_name": "Small",
"package_desc": "AAA",
"package_price": 10,
"package_modules": [
{
"_id": 1,
"subscription_id": 1,
"billing_model": {
"_id": 1,
"unit_count": "Menit",
"counter": 2000
},
"module": {
"_id": 1,
"module_name": "Call",
"status": "Active"
},
"created_at": "2019-06-17T07:59:43.199Z",
},
{
"_id": 2,
"subscription_id": 1,
"billing_model": {
"_id": 3,
"unit_count": "SMS",
"counter": 3000
},
"module": {
"_id": 2,
"module_name": "SMS",
"status": "Active"
},
"created_at": "2019-06-17T08:00:37.464Z",
}
]
},
{
"_id": 2,
"package_name": "Medium",
"package_desc": "BBB",
"package_price": 20,
"package_modules": [
{
"_id": 3,
"subscription_id": 2,
"billing_model": {
"_id": 2,
"unit_count": "Menit",
"counter": 3000
},
"module": {
"_id": "1",
"module_name": "Call",
"status": "Active"
},
"created_at": "2019-06-17T08:01:29.667Z",
},
{
"_id": 4,
"subscription_id": 2,
"billing_model": {
"_id": 4,
"unit_count": "SMS",
"counter": 5000
},
"module": {
"_id": 2,
"module_name": "SMS",
"status": "Active"
},
"created_at": "2019-06-17T08:01:50.285Z",
}
]
}
]
But the output from my code is:
[
{
"_id": "1",
"package_name": "Small",
"package_desc": "AAA",
"package_price": 10,
"package_modules": {
"_id": 1,
"subscription_id": 1,
"billing_model_id": 1
"module_id": 1
"created_at": "2019-06-17T07:59:43.199Z",
},
},
"billing_model": [
{
"_id": 1,
"unit_count": "Menit",
"counter": 2000
},
],
"module": [
{
"_id": 1,
"module_name": "Call",
"status": "Active"
}
]
},
{
"_id": "1",
"package_name": "Small",
"package_desc": "AAA",
"package_price": 10,
"package_modules": {
"_id": 2,
"subscription_id": 2,
"billing_model_id": 3
"module_id": 2
"created_at": "2019-06-17T07:59:43.199Z",
},
},
"billing_model": [
{
"_id": 3,
"unit_count": "SMS",
"counter": 3000
},
],
"module": [
{
"_id": 2,
"module_name": "SMS",
"status": "Active"
}
]
}
..........
// And medium where is loop again
]
I think there are multiple issue in your code.
In your $lookup stage, you need to setup the as field as the package_modules.billing_module and package_modules.module instead of billing_module and module, and so on, so that it becomes a field of package_module object instead of a separate object itself.
you need to unwind after each $lookup stage, as $lookup returns an array instead of an object.
You need to $group at the end of the aggregation pipeline to push all the package_modules of one subscription into one array.
After resolving all the above issues,
Your aggregation pipe should look like this:
Package_subscription
.aggregate([
{
$lookup: {
from: "o_package_modules",
localField: "_id",
foreignField: "subscription_id",
as: "package_modules"
}
},
{
$unwind: {
path: "$package_modules",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "o_modules",
localField: "package_modules.module_id",
foreignField: "_id",
as: 'package_modules.module'
}
},
{
$unwind: {
path: "$package_modules.module",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "o_billing_models",
localField: "package_modules.billing_model_id",
foreignField: "_id",
as: 'package_modules.billing_module'
}
},
{
$unwind: {
path: "$package_modules.billing_module",
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: "$_id",
package_modules : {$push : "$package_modules"},
package_name: {$first . : "$package_name"},
package_desc: {$first . : "$package_desc"},
package_price: {$first . : "$package_price"}
}
}
])
.exec()
.then((pricing) => {
res.json(pricing)
})
.catch((err) => {
res.send(err)
})
The $push in the last $group stage will combine all package_modules into one array. And i think that is what you want at the end.
I hope this works for you.
thank you its working for me
var order_details_data = await OrderDetails.aggregate([{
'$match': {
`order_id`: ObjectId(val._id)
}
}, {
$sort: {
date: -1
}
}, {
$addFields: {
o_price: {
$toDouble: `$o_price`
}
}
}, {
$lookup: {
from: `products`,
localField: `product_id`,
foreignField: `_id`,
as: `product_id`,
},
}, {
$unwind: {
path: `$product_id`,
preserveNullAndEmptyArrays: true
}
}, {
$lookup: {
from: `productvarientvalues`,
localField: `product_id.material_id`,
foreignField: `_id`,
as: `product_id.material_id`,
},
}, {
$unwind: {
path: `$product_id.material_id`,
preserveNullAndEmptyArrays: true
}
}, {
$group: {
_id: `$order_id`,
date: {
$first: `$date`
},
totalAmount: {
$sum: `$totalAmount`
},
orderDetails: {
$push: `$$ROOT`
},
delivery_address: {
$first: delivery_address
},
ship_address_id: {
$first: ship_address_id
},
// material_id: { $push: `$product_id.material_id` }
product_id: {
$push: `$product_id`
},
}
}, {
$project: {
// test: `$test`,
totalAmount: {
$multiply: [`$qty`, `$o_price`]
},
o_price: `$o_price`,
product_id: `$product_id`,
order_id: `$order_id`,
user_id: `$user_id`,
product_id: {
$arrayElemAt: [`$product_id`, 0]
},
data: {
$arrayElemAt: [`$data`, 0]
},
qty: `$qty`,
date: {
$dateToString: {
format: `%M `,
date: `$date`
}
},
discount: `$discount`,
examTotal: {
$sum: [`$qty`, `$price`]
},
createdAt: {
$dateToString: {
format: `%Y-%m-%d %H:%M`,
date: `$createdAt`
}
},
}
}

Resources