I did a lot of research on MongoDB aggregation grouping, but couldn't find a solution.
I have the following document structure:
{
"_id":"6053e0ef22b8e60015a23da8",
"shoppinglist":[
{
"_id":"606ae2e34e4416001538104f",
"items":[
{
"_id":"6071c5ed8f669f0015e6eebe",
"product_id":"605852c28ea29f0015653d6f",
},
...
]
}
}
My goal is to group the items in each shopping list object using the product_id, so that my result looks like this:
{
"_id":"6053e0ef22b8e60015a23da8",
"shoppinglist":[
{
"_id":"606ae2e34e4416001538104f",
"items":[
{
"_id":"6071c5ed8f669f0015e6eebe",
"product_id":"605852c28ea29f0015653d6f",
"count": 3 //3 items with the product_id = 605852c28ea29f0015653d6f
},
...
]
}
}
Can someone help me with this, I'm desperate.
$unwind deconstruct shoppinglist array
$unwind deconstruct shoppinglist.items array
$group by _id and product_id, and get required fields using $first and get count using $sum
$group by _id and shoppinglist._id and reconstruct array of items
$group by _id and reconstruct array of shoppinglist
db.collection.aggregate([
{ $unwind: "$shoppinglist" },
{ $unwind: "$shoppinglist.items" },
{
$group: {
_id: {
_id: "$_id",
product_id: "$shoppinglist.items.product_id"
},
shoppinglist_id: { $first: "$shoppinglist._id" },
items_id: { $first: "$shoppinglist.items._id" },
count: { $sum: 1 }
}
},
{
$group: {
_id: {
_id: "$_id._id",
shoppinglist_id: "$shoppinglist_id"
},
items: {
$push: {
items_id: "$items_id",
product_id: "$_id.product_id",
count: "$count"
}
}
}
},
{
$group: {
_id: "$_id._id",
shoppinglist: {
$push: {
_id: "$_id.shoppinglist_id",
items: "$items"
}
}
}
}
])
Playground
Related
I have collection: bookSchema as:
[
{
_id: ObjectId("637d05dc32428ed75ea08d09"),
book_details: {
book_name: "random123",
book_auth: "Amber"
}
},
{
_id: ObjectId("637d0673ce0f17f6c473dee2"),
book_details: {
book_name: "random321",
book_auth: "Amber"
}
},
{
_id: ObjectId("637d069a3d597c8458ebe4ec"),
book_details: {
book_name: "random676",
book_auth: "Amber"
}
},
{
_id: ObjectId("637d06c05b32d503007bcb54"),
book_details: {
book_name: "random999",
book_auth: "Saurav"
}
}
]
Desired O/P to show as:
{
score_ambr: 3,
score_saurabh: 1
}
For this I tried as:
db.bookSchema.aggregate([
{
"$group": {
"_id": {
"$eq": [
"$book_details.book_auth",
"Amber"
]
},
"score_ambr": {
"$sum": 1
}
},
},
{
"$group": {
"_id": {
"$eq": [
"$book_details.book_auth",
"Saurav"
]
},
"score_saurabh": {
"$sum": 1
}
},
}
])
I tried using $group to as I want to group all the matching documents in one and use $count to give the number of count for the matching documents but it doesn't seem to be working and gives the O/P as
O/P:
[
{
"_id": false,
"score_sau": 2
}
]
MongoDB Playground: https://mongoplayground.net/p/cZ64KwAmwlv
I don't know what mean 3 and 1 in your example but if I've understood correctly you can try this query:
The trick here is to use $facet to create "two ways" in the aggregation. One option will filter by Amber and the other one by Saurav.
And then, as values are filtered, you only need yo know the size of the array generated.
db.collection.aggregate([
{
"$facet": {
"score_ambr": [
{
"$match": {
"book_details.book_auth": "Amber"
}
}
],
"score_saurabh": [
{
"$match": {
"book_details.book_auth": "Saurav"
}
}
]
}
},
{
"$project": {
"score_ambr": {
"$size": "$score_ambr"
},
"score_saurabh": {
"$size": "$score_saurabh"
}
}
}
])
Example here
Note that in this way you avoid to use $group.
It looks like what you want is two group twice and create a dynamic key from the book_details.book_auth:
db.bookSchema.aggregate([
{$group: {_id: "$book_details.book_auth", count: {$sum: 1}}},
{$group: {
_id: 0,
data: {$push: {
k: {$concat: ["score_", {$toLower: "$_id"}]},
v: {$sum: "$count"}
}}
}},
{$replaceRoot: {newRoot: {$arrayToObject: "$data"}}}
])
See how it works on the playground example
I am struggling in Writing that fetches 4 Products of each category. What I have done is
exports.recentproducts = catchAsync(async (req, res, next) => {
const doc = await Product.aggregate([
{ $sort: { date: -1 } },
{
$replaceRoot: {
newRoot: {
$mergeObjects: [{ $arrayElemAt: ['$products', 0] }, '$$ROOT'],
},
},
},
{
$group: {
_id: '$productCategory',
products: { $push: '$$ROOT' },
},
},
{
$project: {
// pagination for products
products: {
$slice: ['$products', 4],
},
_id: 1,
},
},
{
$lookup: {
from: 'Shop',
localField: 'shopId',
foreignField: '_id',
as: 'shop',
},
},
]);
Document Model
const mongoose = require('mongoose');
var ProductSchema = mongoose.Schema({
title: {
type: String,
require: [true, 'Product must have a Title!'],
},
productCategory: {
type: String,
require: [true, 'Product must have a Category!'],
},
shopId: {
type: mongoose.Schema.ObjectId,
ref: 'Shop',
require: [true, 'Product must have a Shop!'],
},
});
var Product = mongoose.model('Product', ProductSchema);
module.exports = Product;
expected result---
result= [
{
productCategory: "Graphics",
products:[//4 products object here
{
must populate shop data
}
]
},
{
productCategory: "3d",
products:[//4 products object here]
},
//there are seven categories I have like that
]
The Code i have done is working fine but it has two problems
It does not populate shopId from Shop Model even through I have tried lookup
It does not sort products in descending order(does not sort by date)
There are few fixes in your implemented query,
$sort stage as it is,
$group stage as it is and moves to the second stage
$project stage as it is and move to third stage
$lookup with shop collection, pass products.shopId as localField
$project for merge shop object in products array
$map to iterate loop of products array
$filter to iterate loop of shop array return matching product
$arrayElemAt to get first element from above filtered result
$mergeOjects to merge current object with filtered shop object
const doc = await Product.aggregate([
{ $sort: { date: -1 } },
{
$group: {
_id: "$productCategory",
products: { $push: "$$ROOT" }
}
},
{
$project: {
products: { $slice: ["$products", 4] }
}
},
{
$lookup: {
from: "Shop",
localField: "products.shopId",
foreignField: "_id",
as: "shop"
}
},
{
$project: {
products: {
$map: {
input: "$products",
in: {
$mergeObjects: [
"$$this",
{
shop: {
$arrayElemAt: [
{
$filter: {
input: "$shop",
as: "s",
cond: { $eq: ["$$s._id", "$$this.shopId"] }
}
},
0
]
}
}
]
}
}
}
}
}
])
Playground
Query
in MongoDB 5 we can use $setWindowFields and $rank
partition by productCategory and sort by date descending
keep only rank <= 4 (4 latest products)
lookup to get the shop information
group by category and push all the information of product and shop
Test code here
Product.aggregate(
[{$setWindowFields:
{partitionBy:"$productCategory",
sortBy:{date:-1},
output:{rank:{$rank:{}}}}},
{$match:{rank:{$lte:4}}},
{$lookup:
{from:"Shop",
localField:"shopId",
foreignField:"_id",
as:"shop"}},
{$set:{shop:{$first:"$shop"}}},
{$group:{_id:"$productCategory", products:{$push:"$$ROOT"}}}])
I am looking for a query for a $match stage in my aggregation which do almost the same, as in this question, but..
if field (named rank in my case) doesn't exists in document, add document to results
but if field, exists, apply $operator condition (in my case it's $max) to this field, and add all documents that suits this condition to the results.
MongoPlayground with example collection.
Result should be like this:
[
{
"method": 3,
"item": 1,
"rank": 3 //because it has field named rank, and suits condition {rank: $max}
},
{
"method": 4,
"item": 1 //we need this, because document doesn't have rank field at all
},
{
"method": 5,
"item": 1 //we need this, because document doesn't have rank field at all
}
]
Things, that I have tried already:
{
$match: {
$or: [
{item: id, rank: {$exists: true, $max: "$rank"}}, //id === 1
{item: id, rank: {$exists: false}} //id === 1
]
}
}
UPD: As for now, probably I don't limit with $match stage only, $project is also relevant after default match, so I could request every document during $match stage by id no matter, have the doc rank field or not, and then, during $project stage do a "separation" by rank $exists
Try this one:
db.collection.aggregate([
{
$match: {
item: id
}
},
{
$group: {
_id: "$item", //<- Change here your searching field
max: {
$max: "$rank" //<- Change here your field to apply $max
},
data: {
$push: "$$ROOT"
}
}
},
{
$unwind: "$data"
},
{
$match: {
$expr: {
$or: [
{
$eq: [
{
$type: "$data.rank"
},
"missing"
]
},
{
$eq: [
"$data.rank",
"$max"
]
}
]
}
}
},
{
$replaceWith: "$data"
}
])
MongoPlayground
I have found an answer, separated from #Valijon's method, but it's also based on the logic above. My query is:
db.collection.aggregate([
{
$match: {
item: id
}
},
{
$project: {
method: 1,
item: 1,
rank: {
$ifNull: [
"$rank",
0
]
}
}
},
{
$group: {
_id: "$item",
data: {
$addToSet: "$$ROOT"
},
min_value: {
$min: "$rank"
},
max_value: {
$max: "$rank"
}
}
},
{
$unwind: "$data"
},
{
$match: {
$or: [
{
$expr: {
$eq: [
"$data.rank",
"$max_value"
]
}
},
{
$expr: {
$eq: [
"$data.rank",
"$min_value"
]
}
},
]
}
}
])
My query is based on $project stage which gives the empty field value 0. It also could be -1, or any value that isn't used in collection. And then I separate results.
MongoPlayground
update so Mohammad Faisal has the best solution.However it breaks when a new document is added lol! so i learned a lot from his code and modified it and it Works! =) the code is all the way in the bottom.
But here's what i said..
So i have this document
{"_id":"5ddea2e44eb407059828d740",
"projectname":"wdym",
"username":"easy",
"likes":0,
"link":["ssss"]
}
{"_id":"5ddea2e44eb407059822d740",
"projectname":"thechosenone",
"username":"easy",
"likes":30,
"link":["ssss"]
}
{"_id":"5ddea2e44eb407059828d740",
"projectname":"thanos",
"username":"wiley",
"likes":10,
"link":["ssss"]
}
and basically what i want is the document that contains the highest
likes with it's associated project name
For example the output would be
"projectname":"thechosenone",
"username":"easy",
"likes":30
}
,
{
"projectname":"thanos",
"username":"wiley",
"likes":10,
}
the code i have for this is the following
db
.collection("projects")
.aggregate([
{
$group: {
_id: { username: "$username" },
likes: { $max: "$likes" }
}
},
{
$project:{projectname:1}
}
])
$project gives me a strange output. However,
the output was correct without the $project.
But i wanted to project the projectname, the user and the highest likes. Thanks for hearing me out :)
heres the solution =)
db
.collection("projects")
.aggregate([
{
$sort: {
likes: -1
}
},
{
$group: {
_id: {
username: "$username"
},
likes: {
$max: "$likes"
},
projectname: {
$push: "$projectname"
},
link: {
$push: "$link"
}
}
},
{
$project: {
username: "$_id.username",
projectname: {
$arrayElemAt: ["$projectname", 0]
},
link: {
$arrayElemAt: ["$link", 0]
}
}
}
])
.toArray()
If you don't have to use $group this will solve your problem:
db.projects.aggregate([
{$sort:{likes:-1}},
{$limit:1}
]).pretty()
the result would be
{
"_id" : ObjectId("5ddee7f63cee7cdf247059db"),
"projectname" : "thechosenone",
"username" : "easy",
"likes" : 30,
"links" : ["ssss"]
}
Try this:-
db.collection("projects").aggregate([
{
$group: {
_id: { username: "$username" },
likes: { $max: "$likes" },
projectname: { $push : { $cond: [ { $max: "$likes" }, "$projectname", "" ]}}
}
}
,
{
$project:{
username:"$_id.username",
projectname:{"$reduce": {
"input": "$projectname",
"initialValue": { "$arrayElemAt": ["$projectname", 0] },
"in": { "$cond": [{ "$ne": ["$$this", ""] }, "$$this", "$$value"] }
}},
likes:1
}
}
])
I am new to MongoDB and I am stuck in the below scenario.
I have a collection that contains duplicate docs.
I just want to get the sum of the property in each doc excluding the duplicate docs.
My Docs looks like this:
{"_id":"5dd629461fc50b782479ea90",
"referenceId":"5dd581f10859d2737965d23a",
"sellingId":"319723fb80b1a297cf0803abad9bc60787537f14a6a37d6e47",
"account_name":"mrfsahas1234",
"vendor_name":"testaccount2",
"action_type":"purchase",
"product_name":"Bottle",
"product_quantity":10,
"transactionId":"319723fb80b1a297cf0803abad9bc60787537f14a6a37d6e47",
"uid":"2019-11-20T17:39:17.405Z",
"createdAt":"2019-11-21T08:56:56.589+00:00",
"updatedAt":"2019-11-21T08:56:56.589+00:00","__v":0
},
{
"_id":"5dd629461fc50b782479ea90",
"referenceId":"5dd581f10859d2737965d23a",
"sellingId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
"account_name":"mrfsahas1234",
"vendor_name":"testaccount2",
"action_type":"purchase",
"product_name":"Bottle",
"product_quantity":50,
"transactionId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
"uid":"2019-11-20T17:39:17.405Z",
},
{
"_id":"5dd629461fc50b782479ea90",
"referenceId":"5dd581f10859d2737965d23a",
"sellingId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
"account_name":"mrfsahas1234",
"vendor_name":"testaccount2",
"action_type":"purchase",
"product_name":"Bottle",
"product_quantity":50,
"transactionId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
"uid":"2019-11-20T17:39:17.405Z",
},
Currently, I am doing this:
MaterialsTrack.aggregate([
{
$match: {
$and: [
{product_name: product_name},
{account_name: account_name},
{action_type: 'purchase'},
{uid:uid}
]
}
},
{
$group: {_id: "$sellingId", PurchseQuantity: {$sum: "$product_quantity"}}
},
])
It returns the sum of product_quantity all the matching docs (including the duplicate docs).
Current Output:
{_id: "320a9a2f814a45e01eb98344c9af708fa2864d81587e5914", PurchseQuantity:110}
Expected Output:
{_id: "320a9a2f814a45e01eb98344c9af708fa2864d81587e5914", PurchseQuantity:60}
I want to get the sum of only unique docs. How can I achieve it?
Thanks in advance!
You need to sum inside of the $group _id field, and then use the replaceRoot to achieve the the result you wanted.
MaterialsTrack.aggregate([
{
$match: {
$and: [
{
product_name: "Bottle"
},
{
account_name: "mrfsahas1234"
},
{
action_type: "purchase"
},
{
uid: "2019-11-20T17:39:17.405Z"
}
]
}
},
{
$group: {
_id: {
sellingId: "$sellingId",
PurchaseQuantity: {
$sum: "$product_quantity"
}
}
}
},
{
$replaceRoot: {
newRoot: {
_id: "$_id.sellingId",
PurchaseQuantity: "$_id.PurchaseQuantity"
}
}
}
]);
Sample Output:
[
{
"PurchaseQuantity": 50,
"_id": "320a9a2f814a45e01eb98344c9af708fa2864d81587e5914"
}
]
Playground:
https://mongoplayground.net/p/MOneCRiSlO0
What about adding $addToSet to your aggregations pipeline
MaterialsTrack.aggregate([
{
$match: {
$and: [
{product_name: product_name},
{account_name: account_name},
{action_type: 'purchase'},
{uid:uid}
]
}
},
{
$group: {_id: "$sellingId", PurchseQuantity: {$sum: "$product_quantity"},"list" : {$addToSet : "$list"}}
},
])