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`
}
},
}
}
Related
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": ""
}
i want to add isFavorite as true/false, if user mark that combination as favorite."isFavorite" : 0 means user already marked that item as favorite.my query is
db.getCollection('itemorders').aggregate([
{ "$match": {"customerId" : ObjectId("5e78a07c0ce36c23dcf85e6a"), 'isCart': 0}},
{ "$lookup": {
"from": "itemlistorders",
"let": { "cartId": { "$toObjectId" : "$_id" }},
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$cartId", "$$cartId"] }}},
],
"as": "itemOrdered"
} },
{$unwind: { path: "$itemOrdered", preserveNullAndEmptyArrays: true }},
{ "$lookup": {
"from": "favoritedrinks",
"let": { "uniqueCartKey": "$itemOrdered.uniqueCartKey" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$uniqueFavKey", "$$uniqueCartKey"] }}},
{ "$addFields": { "isFavorite": true }}
],
"as": "mixerList"
} },
{$unwind: { path: "$itemOrdered", preserveNullAndEmptyArrays: true }},
{
"$group": {
"_id": "$_id",
"grantTotal" : { "$first": "$grantTotal" },
"customerName" : { "$first": "$customerName" },
"barName" : { "$first": "$barName" },
"itemOrdered": { "$addToSet": "$itemOrdered" },
}
},{
$project: {
'_id': 1,
'grantTotal': 1,
'customerName': 1,
'barName': 1,
'itemOrdered': 1,
}
},
]);
My output after excusting this is :
{
"_id" : ObjectId("5e8224768910e83e908d0108"),
"itemOrdered" : [
{
"_id" : ObjectId("5e82257250c82e1abc16d856"),
"barId" : ObjectId("5e55f60eff2f842de4ae6184"),
"customerId" : ObjectId("5e78a07c0ce36c23dcf85e6a"),
"isCart" : 0,
"isAlcoholActive" : 1,
"alcoholId" : "5e81d13e50c82e1abc0a1ebb",
"alcoholName" : "Irish Coffee",
"cartId" : ObjectId("5e8224768910e83e908d0108"),
"itemTotal" : 250,
"noofDrinks" : 2
},
{
"_id" : ObjectId("5e8224e88910e83e908d0110"),
"isCart" : 0,
"isAlcoholActive" : 1,
"isFavorite" : 0,
"customerId" : ObjectId("5e78a07c0ce36c23dcf85e6a"),
"barId" : ObjectId("5e55f60eff2f842de4ae6184"),
"noofDrinks" : 1,
"itemTotal" : 200,
"mixerList" : [
{
"isMixerActive" : 0,
"_id" : ObjectId("5e8224e88910e83e908d0111"),
"mixerId" : "5e820b0250c82e1abc12cffc",
"mixerName" : "Pineapple juice"
}
],
"alcoholId" : "5e81d17150c82e1abc0a2607",
"alcoholName" : "Fireball",
"cartId" : ObjectId("5e8224768910e83e908d0108"),
}
]
};
i want to add isFavorite in each subdocument,so that i can identify whether user favorite this item or not.
below query worked for my scenerio.
db.getCollection('itemorders').aggregate([
{ "$match": {"customerId" : ObjectId("5e78a07c0ce36c23dcf85e6a"), 'isCart': 0}},
{ "$lookup": {
"from": "itemlistorders",
"let": { "cartId": { "$toObjectId" : "$_id" }},
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$cartId", "$$cartId"] }}},
],
"as": "itemOrdered"
} },
{$unwind: { path: "$itemOrdered", preserveNullAndEmptyArrays: true }},
{ "$lookup": {
"from": "favoritedrinks",
"let": { "uniqueCartKey": "$itemOrdered.uniqueCartKey","customerId1": "$itemOrdered.customerId", "modifierfavId": "$itemOrdered._id" },
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$uniqueFavKey", "$$uniqueCartKey"
]
},
{
$eq: [
"$customerId",
"$$customerId1"
]
}
]
}
}
},{ "$addFields": { "isFavorite": 'true' }}
],
"as": "itemListOrdered"
} },
{ "$addFields": { "itemOrdered.isFavorite": { "$gt": ["$itemOrdered.isFavorite", null] } }},
{
"$group": {
"_id": "$_id",
"grantTotal" : { "$first": "$grantTotal" },
"customerName" : { "$first": "$customerName" },
"barName" : { "$first": "$barName" },
"orderDate" : { "$first": "$orderDate" },
"itemOrdered": { "$push": "$itemOrdered" },
}
}
])
I am trying to sort by the task._id & date in desc order. I am able to sort by task._id but sortibg bydate doesnt work, I tried changing the order in aggregate still no luck. I get the response but just the order by usertasks were added in collection and not by the usertask.date
User(name, address, etc)
Task(name, icon, assignee)
UserTask(User.ObjectId, Task.ObjectId, date)
User Collection:
{
"users": [
{
"name": "Bill",
"phone": "345"
},
{
"name": "Steve",
"phone": "123"
},
{
"name": "Elon",
"phone": "567"
}
]
}
Task collection:
{
"tasks": [
{
"name": "Run 100m",
"icon": "run",
"assignee": "Elon"
},
{
"name": "Walk 1 hour",
"icon": "walk",
"assignee": "Bill"
},
{
"name": "Jog 30 minutes",
"icon": "jog",
"assignee": "Steve"
}
]
}
UserTasks:
{
"_id": "5e72fec..",
"user": "5e72fa4..",
"task": "5e72fbac..",
"date": "2020-03-03T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:37.027Z",
"updatedAt": "2020-03-19T05:10:37.027Z",
"__v": 0
},
{
"_id": "5e72fed3..",
"user": "5e72fa4e..",
"task": "5e72fbac..",
"date": "2020-03-12T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:43.296Z",
"updatedAt": "2020-03-19T05:10:43.296Z",
"__v": 0
},
{
"_id": "5e72fed6..",
"user": "5e72fa..",
"task": "5e72fb..",
"date": "2020-03-15T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:46.057Z",
"updatedAt": "2020-03-19T05:10:46.057Z",
"__v": 0
},
{
"_id": "5e72feda...",
"user": "5e72fa4..",
"task": "5e72fb..",
"date": "2020-03-07T05:10:10.000Z",
"createdAt": "2020-03-19T05:10:50.785Z",
"updatedAt": "2020-03-19T05:10:50.785Z",
"__v": 0
}
This is the Aggregate that needs changing
UserTask.aggregate([
{
$lookup: {
from: "tasks",
localField: "task",
foreignField: "_id",
as: "matchedTask"
}
},
{
$unwind: "$matchedTask"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "matchedUser"
}
},
{
$unwind: "$matchedUser"
},
{
$group: {
_id: "$matchedTask._id",
name: {$first: "$matchedTask.name"},
icon: {$first: "$matchedTask.icon"},
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
date: "$date"
}
}
}
},
{
$sort: { _id: 1, "userdata.date": -1 }
}
])
.exec()
.then(doc => res.status(200).json(doc))
.catch(err => res.status(400).json("Error: " + err));
The response is shown below, please note the usertask.date. it is NOT sorted
{
"_id": "5e...",
"name": "Run 100m",
"icon": "run",
"assignee": "Elon",
"userdata": [
{
"name": "Elon",
"date": "2020-03-21T20:02:38.143Z"
},
{
"name": "Bill",
"date": "2020-03-11T20:02:38.000Z"
},
{
"name": "Steve",
"date": "2020-03-19T20:02:38.000Z"
}
]
}
As you can see the it is not sorted by date - desc order. The result should be like shown below
"userdata": [
{
"name": "Elon",
"date": "2020-03-21T20:02:38.143Z"
},
{
"name": "Steve",
"date": "2020-03-19T20:02:38.000Z"
},
{
"name": "Bill",
"date": "2020-03-11T20:02:38.000Z"
}
]
$sort will use the last object which comes out from the aggregate pipe and theres no field "date" in this object:
{
$group: {
_id: "$matchedTask._id",
name: {$first: "$matchedTask.name"},
icon: {$first: "$matchedTask.icon"},
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
execDate: "$date"
}
}
}
},
your group has to returned a field named date in order to be able to sort it
{
$group: {
_id: "$matchedTask._id",
name: ....,
date: ....
}
}
{ $sort: {date: -1}}
if the value you want to sort is indide another object you must specify it on sort:
{$sort: {"userdata.date": -1}}
I fixed it, had to use sort two times, now I am able to get the result as I want
Solution provided below
UserTask.aggregate([
{
$lookup: {
from: "tasks",
localField: "task",
foreignField: "_id",
as: "matchedTask"
}
},
{
$unwind: "$matchedTask"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "matchedUser"
}
},
{
$unwind: "$matchedUser"
},
{
$sort: { date: -1 }
},
{
$group: {
_id: "$matchedTask._id",
name: { $first: "$matchedTask.name" },
icon: { $first: "$matchedTask.icon" },
assignee: { $first: "$matchedTask.assignee" },
userdata: {
$push: {
name: "$matchedUser.name",
execDate: "$date"
}
}
}
},
{
$sort: { _id: 1 }
}
])
.exec()
.then(doc => res.status(200).json(doc))
.catch(err => res.status(400).json("Error: " + err));
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.
I need some help with Mongo, Mongoose and Node.js.
In the code below, I'd like to join carrinho and produtos collection to retrieve produtos _id, price and description in the same array/object.
My Carrinho Schema
const Carrinho = new mongoose.Schema(
{
title: {
type: String,
},
produtos: [{
price: Number,
produto: { type: mongoose.Schema.Types.ObjectId, ref:
"Produtos" }
}
],
total: {
type: Number,
},
},
{
timestamps: true
})
My Produtos Schema
const Produtos = new mongoose.Schema(
{
description: {
type: String,
required: true,
},
gtin: {
type: String,
required: true,
unique: true,
},
thumbnail: {
type: String,
},
price: {
type: Number,
}
},
{
timestamps: true
}
)
After reading aggregate documentation this is the best I've got:
Carrinho.aggregate([
{ "$match": { "_id": mongoose.Types.ObjectId(req.params.id) } },
{
"$lookup": {
"from": "produtos",
"localField": "produtos._id",
"foreignField": "_id",
"as": "produtosnocarrinho"
}
},
{
"$addFields": {
"total": {
"$reduce": {
"input": "$produtos",
"initialValue": 0,
"in": { "$add": ["$$value", "$$this.price"] }
}
}
}
}
]).exec((err, data) => {
if (err) res.json(err)
res.json(data)
});
And this is the result:
[
{
"_id": "5cb76d7d99c3f4062f512537",
"title": "Carrinho do Lucas",
"produtos": [
{
"_id": "5cafead2bc648978100d7698",
"price": 20.1
},
{
"_id": "5cae911adf75ac4d3ca4bcb6",
"price": 20.1
},
{
"_id": "5cb0f0adc5fb29105d271499",
"price": 20.1
}
],
"createdAt": "2019-04-17T18:16:29.833Z",
"updatedAt": "2019-04-19T00:50:43.316Z",
"__v": 3,
"produtosnocarrinho": [
{
"_id": "5cae911adf75ac4d3ca4bcb6",
"description": "AÇÚCAR REFINADO UNIÃO 1KGS",
"gtin": "7891910000197",
"thumbnail": "7891910000197",
"createdAt": "2019-04-11T00:58:02.296Z",
"updatedAt": "2019-04-11T00:58:02.296Z",
"__v": 0
},
{
"_id": "5cafead2bc648978100d7698",
"description": "HASBRO MR. POTATO HEAD MALETA DE PEÇAS",
"gtin": "5010994598815",
"thumbnail": "pecas_300x300-PU3435f_1.jpg",
"createdAt": "2019-04-12T01:33:06.628Z",
"updatedAt": "2019-04-12T01:33:06.628Z",
"__v": 0
},
{
"_id": "5cb0f0adc5fb29105d271499",
"description": "REPELENTE EXPOSIS INFANTIL SPRAY",
"gtin": "7898392800055",
"thumbnail": "PU28bb9_1.jpg",
"createdAt": "2019-04-12T20:10:21.363Z",
"updatedAt": "2019-04-12T20:10:21.363Z",
"__v": 0
}
],
"total": 60.300000000000004
}
]
The following Query will be help:
models.Carrinho.aggregate(
[
{ "$match": { "_id": mongoose.Types.ObjectId(req.params.id) } },
{
"$lookup": {
"from": "produtos",
"localField": "produtos._id",
"foreignField": "_id",
"as": "produtosnocarrinho"
}
},
{
"$addFields": {
"total": {
"$reduce": {
"input": "$produtos",
"initialValue": 0,
"in": { "$add": ["$$value", "$$this.price"] }
}
}
}
},
{$unwind : '$produtos'},
{$unwind : '$produtosnocarrinho'},
{$redact: { $cond: [{
$eq: [
"$produtos._id",
"$produtosnocarrinho._id"
]
},
"$$KEEP",
"$$PRUNE"
]
}
},
{ $project: {
_id : 1,
title : 1,
produtosData : {
_id : "$produtos._id",
price : "$produtos.price",
description : "$produtosnocarrinho.description"
},
total : 1,
createdAt: 1,
updatedAt : 1
}
},
{
$group : {
_id : {
_id : '$_id',
title : '$title',
total : '$total',
createdAt : '$createdAt',
updatedAt : '$updatedAt'
},
produtosData: {$push: "$produtosData" }
}
},
{ $project: {
_id : '$_id._id',
title : '$_id.title',
total : '$_id.total',
createdAt : '$_id.createdAt',
updatedAt : '$_id.updatedAt',
produtosData: '$produtosData'
}
}
]).exec((err, data) => {
if (err) res.json(err)
res.json(data)
});
Output :
[{
"_id": "5cbc42c24502a7318952d7b2",
"title": "Carrinho do Lucas",
"total": 60.300000000000004,
"createdAt": "2019-04-21T10:15:30.629Z",
"updatedAt": "2019-04-21T10:15:30.629Z",
"produtosData": [{
"_id": "5cafead2bc648978100d7698",
"price": 20.1,
"description": "HASBRO MR. POTATO HEAD MALETA DE PEÇAS"
}, {
"_id": "5cae911adf75ac4d3ca4bcb6",
"price": 20.1,
"description": "AÇÚCAR REFINADO UNIÃO 1KGS"
}, {
"_id": "5cb0f0adc5fb29105d271499",
"price": 20.1,
"description": "REPELENTE EXPOSIS INFANTIL SPRAY"
}]
}]
performance depends on produtos matching data from Lookup Query As we are doing double Unwind.