Mongo group inside $addToSet - mongodb

I have the following set of objects:
[
{
id: 1,
clientId: 1,
cost: 200
},
{
id: 1,
clientId: 2,
cost: 500
},
{
id: 1,
clientId: 2,
cost: 800
},
{
id: 2,
clientId: 1,
cost: 600
},
{
id: 2,
clientId: 2,
cost: 100
}
]
And I made a group of that with:
db.collection.aggregate(
{
'$group': {
'_id': '$id',
'clients': {
'$addToSet': {
'id': '$clientId',
'cost': '$cost'
}
}
}
}
)
So I obteined the following:
[
{
'_id': 1,
'clients': [
{
id: 1,
cost: 200
},
{
id: 2,
cost: 500
},
{
id: 2,
cost: 800
}
],
'_id': 2,
'clients': [
{
id: 1,
cost: 600
},
{
id: 2,
cost: 100
}
]
}
]
As you can see in the array of clients of the first value, I have 2 repeated and what I want is to have 1 with the cost added. So instead of have:
'clients': [
{
id: 1,
cost: 200
},
{
id: 2,
cost: 500
},
{
id: 2,
cost: 800
}
]
I need:
'clients': [
{
id: 1,
cost: 200
},
{
id: 2,
cost: 1300
}
]
So my question is: how can I do that? Because $addToSet nor $push allow $sum.

You can use aggregation operators to get expected output like following:
db.collection.aggregate({
"$group": {
"_id": {
"mainId": "$id",
"client": "$clientId"
},
"cost": {
"$sum": "$cost"
}
}
}, {
"$project": {
"mainId": "$_id.mainId",
"clients": {
"clientId": "$_id.client",
"cost": "$cost"
},
"_id": 0
}
}, {
"$group": {
"_id": "$mainId",
"clients": {
"$push": "$clients"
}
}
})

Related

Mongodb aggregation pipeline, combine result from two facet pipeline

I'm having a claim type:
type TClaim: {
insuredId: number,
treatmentInfo: { amount: number }[]
}
and a list of claims:
[
{
insuredId: 1,
treatmentInfo: [{amount: 1}, {amount: 2}]
},
{
insuredId: 1,
treatmentInfo: [{amount: 3}, {amount: 4}]
},
{
insuredId: 2,
treatmentInfo: [{amount: 1}, {amount: 2}]
}
]
I want to get the result like:
[{insuredId: 1, numberOfClaims: 2, amount: 10},{insuredId: 2, numberOfClaims: 1, amount: 3}]
I'm using the $facet operator in mongodb aggregation, one for counting numberOfClaims and one for calculating the amount of each insurer. But I can't combine it to get the result that I want.
$facet: {
totalClaims: [ { $group: { _id: '$insuredId', totalClaims: { $count: {} } } } ],
amount: [ { $unwind: { path: '$treatmentInfo'}},
{ $group:
{ _id: '$insuredId',
amount: { $sum: '$treatmentInfo.amount',
},
},
},
]
Is there a reason why you want to use $facet? - I am just curious
You just need to add a new fields that sums up all the amount in the array first and then do a group stage by insuredId. The query is pretty much self-explanatory.
db.collection.aggregate([
{
"$addFields": {
"totalAmount": {
"$sum": "$treatmentInfo.amount"
}
}
},
{
"$group": {
"_id": "$insuredId",
"numberOfClaims": {
"$sum": 1
},
"amount": {
"$sum": "$totalAmount"
}
}
}
])
Result:
[
{
"_id": 1,
"amount": 10,
"numberOfClaims": 2
},
{
"_id": 2,
"amount": 3,
"numberOfClaims": 1
}
]
MongoDB Playground

get rank in mongodb with date range

I have following stat data stored daily for users.
{
"_id": {
"$oid": "638df4e42332386e0e06d322"
},
"appointment_count": 1,
"item_id": 2,
"item_type": "user",
"company_id": 5,
"created_date": "2022-12-05",
"customer_count": 1,
"lead_count": 1,
"door_knocks": 10
}
{
"_id": {
"$oid": "638f59a9bf33442a57c3aa99"
},
"lead_count": 2,
"item_id": 2,
"item_type": "user",
"company_id": 5,
"created_date": "2022-12-06",
"video_viewed": 2,
"door_knocks": 9
}
And I'm using the following query to get the items by rank
user_stats_2022_12.aggregate([{"$match":{"company_id":5,"created_date":{"$gte":"2022-12-04","$lte":"2022-12-06"}}},{"$setWindowFields":{"partitionBy":"$company_id","sortBy":{"door_knocks":-1},"output":{"item_rank":{"$denseRank":{}},"stat_sum":{"$sum":"$door_knocks"}}}},{"$facet":{"metadata":[{"$count":"total"}],"data":[{"$skip":0},{"$limit":100},{"$sort":{"item_rank":1}}]}}])
It's giving me the rank but with the above data, the record with item_id: 2 are having different rank for same item_id. So I wanted to group them by item_id and then applied rank.
It's a little messy, but here's a playground - https://mongoplayground.net/p/JrJOo4cl9X1.
If you're going to sort by knocks after grouping, I'm assuming that you'll want the sum of door_knocks for a given item_id for this sort.
db.collection.aggregate([
{
$match: {
company_id: 5,
created_date: {
"$gte": "2022-12-04",
"$lte": "2022-12-06"
}
}
},
{
$group: {
_id: {
item_id: "$item_id",
company_id: "$company_id"
},
docs: {
$push: "$$ROOT"
},
total_door_knocks: {
$sum: "$door_knocks"
}
}
},
{
$setWindowFields: {
partitionBy: "$company_id",
sortBy: {
total_door_knocks: -1
},
output: {
item_rank: {
"$denseRank": {}
},
stat_sum: {
"$sum": "$total_door_knocks"
}
}
}
},
{
$unwind: "$docs"
},
{
$project: {
_id: "$docs._id",
appointment_count: "$docs.appointment_count",
company_id: "$docs.company_id",
created_date: "$docs.created_date",
customer_count: "$docs.customer_count",
door_knocks: "$docs.door_knocks",
item_id: "$docs.item_id",
item_type: "$docs.item_type",
lead_count: "$docs.lead_count",
item_rank: 1,
stat_sum: 1,
total_door_knocks: 1
}
},
{
$facet: {
metadata: [
{
"$count": "total"
}
],
data: [
{
"$skip": 0
},
{
"$limit": 100
},
{
"$sort": {
"item_rank": 1
}
}
]
}
}
])

mongoDb groupby with array object field

I have searched a lot to use groupby based on the array field value, but I didn't get proper results in google, so I'm posting here.
I have tried my best, it works 50% need to correct my query can anyone help me with this
I have a database value like
{"_id": "62b0bec8922dc767f8b933b4",
"seatSeletion": [{
"rowNo": 0,
"columnNo": 0,
"seatNo": 3
}, {
"rowNo": 0,
"columnNo": 1,
"seatNo": 4
}],
"movieId": "62af1ff6cb38656a4ffe36aa",
"movieDate": "2022-06-20T18:14:38.133+00:00",
"movieTiming": "1:30 p.m",
},
{"_id": "62b0b91560f57e0cb220db02","seatSeletion": [{
"rowNo": 0,
"columnNo": 0,
"seatNo": 1
}, {
"rowNo": 0,
"columnNo": 1,
"seatNo": 2
}],
"movieId": "62af1ff6cb38656a4ffe36aa",
"movieDate": "2022-06-20T18:14:38.133+00:00",
"movieTiming": "1:30 p.m",
}
expected output
{
"seatSeletion": [
{
"rowNo": 0,
"columnNo": 0,
"seatNo": 1
},
{
"rowNo": 0,
"columnNo": 1,
"seatNo": 2
},
{
"_id": "62b0b90e60f57e0cb220db00",
"rowNo": 0,
"columnNo": 0,
"seatNo": 3
},
{
"_id": "62b0b90e60f57e0cb220db01",
"rowNo": 0,
"columnNo": 1,
"seatNo": 4
}
],
"movieTiming": "1:30 p.m",
"movieId": "62af1ff6cb38656a4ffe36aa",
"movieDate": "2022-06-20T11:03:37.000Z"
},
this is how I tried in my query
Bookings.aggregate([
{
$match: {
$and: [{ movieId: ObjectId(bookingParam.movieId) },
{ movieTiming: bookingParam.movieTiming },
{ movieDate: dateQuery },
]
}
},
{
$group: {
_id: {
seatSeletion: '$seatSeletion', movieTiming: '$movieTiming',
movieId: '$movieId', movieDate: '$movieDate', createdBy: "$createdBy", updatedBy: "$updatedBy", movies: "$movies"
}
}
},
{
$project: {
seatSeletion: '$_id.seatSeletion', movieTiming: '$_id.movieTiming',
movieId: '$_id.movieId', movieDate: '$_id.movieDate', movies: "$_id.movies",
_id: 0
}
}
])
but i got it like this
{
"seatSeletion": [
{
"_id": "62b0b91560f57e0cb220db03",
"rowNo": 0,
"columnNo": 0,
"seatNo": 1
},
{
"_id": "62b0b91560f57e0cb220db04",
"rowNo": 0,
"columnNo": 1,
"seatNo": 2
}
],
"movieTiming": "1:30 p.m",
"movieId": "62af1ff6cb38656a4ffe36aa",
"movieDate": "2022-06-20T11:03:37.000Z"
},
{
"seatSeletion": [
{
"_id": "62b0b90e60f57e0cb220db00",
"rowNo": 0,
"columnNo": 0,
"seatNo": 3
},
{
"_id": "62b0b90e60f57e0cb220db01",
"rowNo": 0,
"columnNo": 1,
"seatNo": 4
}
],
"movieTiming": "1:30 p.m",
"movieId": "62af1ff6cb38656a4ffe36aa",
"movieDate": "2022-06-20T11:03:37.000Z"
}
can anyone help me to fix this issue.
One option is using $reduce after the $group. It is important NOT to group by the seatSeletion as the value of this field is not common to these movies:
db.collection.aggregate([
{
$match: {
$and: [
{movieId: "62af1ff6cb38656a4ffe36aa"},
{movieTiming: "1:30 p.m"},
{movieDate: "2022-06-20T18:14:38.133+00:00"},
]
}
},
{
$group: {
_id: {movieTiming: "$movieTiming", movieId: "$movieId", movieDate: "$movieDate"},
seatSeletion: {$push: "$seatSeletion"}
}
},
{
$project: {
seatSeletion: {
$reduce: {
input: "$seatSeletion",
initialValue: [],
in: {$concatArrays: ["$$value", "$$this"]}
}
},
movieTiming: "$_id.movieTiming",
movieId: "$_id.movieId",
movieDate: "$_id.movieDate",
_id: 0
}
}
])
See how it works on the playground example
Another option is using $unwind instead of $reduce, but it is generally considered slower:
db.collection.aggregate([
{
$match: {
$and: [
{movieId: "62af1ff6cb38656a4ffe36aa"},
{movieTiming: "1:30 p.m"},
{movieDate: "2022-06-20T18:14:38.133+00:00"},
]
}
},
{$unwind: "$seatSeletion"},
{
$group: {
_id: {movieTiming: "$movieTiming", movieId: "$movieId", movieDate: "$movieDate"},
seatSeletion: {$push: "$seatSeletion"}
}
},
{
$project: {
seatSeletion: 1,
movieTiming: "$_id.movieTiming",
movieId: "$_id.movieId",
movieDate: "$_id.movieDate",
_id: 0
}
}
])
See how it works on the playground example - unwind
more output nearly you expect
{
"_id": {
"movieId": "62af1ff6cb38656a4ffe36aa",
"movieDate": "2022-06-20T18:14:38.133+00:00",
"movieTiming": "1:30 p.m"
},
"seatSeletion": [
{ "rowNo": 0,"columnNo": 0,"seatNo": 3
},
{ "rowNo": 0,"columnNo": 1,"seatNo": 4
},
{ "rowNo": 0,"columnNo": 0,"seatNo": 1
},
{ "rowNo": 0,"columnNo": 1,"seatNo": 2
}
]
}
query
db.collection.aggregate(
{
$match: {}
},
{
$unwind: {
path: '$seatSeletion'
}
},
{
$group: {
_id:
{
movieId: '$movieId',
movieDate: '$movieDate',
movieTiming: '$movieTiming'
},
seatSeletion:
{ $push: '$seatSeletion' }
}
}
)

How to convert timestamp to date in an array with Mongodb?

I need to convert the timestamp to date and update it in the same field, in Mongosh.
Here is the data:
{ _id: 1,
name: 'Annelise',
movies: [ { movieid: 12, rating: 3, timestamp: 97830291 } ] },
{ _id: 2,
name: 'Maria',
movies: [ { movieid: 3, rating: 3, timestamp: 978301398 } ]
},
{
_id: 3,
name: 'Paul',
movies: [ { movieid: 23, rating: 4, timestamp: 978302174 } ]
},
{
_id: 4,
name: 'Fred',
movies: [
{ movieid: 23, rating: 4, timestamp: 978302174 },
{ moviedid: 45, rating: 2, timestamp: 978302149 }
]
},
{ _id: 5, name: 'Annelise', timestamp: 97830291 },
{ _id: 6, name: 'Maria', timestamp: 978301398 },
{ _id: 7, name: 'Paul', timestamp: 978302174 },
{ _id: 8, name: 'Fred', timestamp: 978302149 }
I tried using an aggregation pipeline, but I have 2 problems:
This command works to update the timestamps for _id 5 to 8, but how can I save it to the same field?
db.usertest.aggregate(
[ {"$project":
{"timestamp": { $dateToString: { format: "%Y-%m-%d", date: { "$toDate": {$toLong:"$timestamp"}}}}
} } ])
How can I update the timestamp inside the movies array for _id 1 to 4?
I tried the following but this does not work:
db.usertest.aggregate( [ {"$project": {"timestamp": { $dateToString: { format: "%Y-%m-%d", date: { "$toDate": {$toLong:"$timestamp"}}}}}} ])
Thanks for your help!
You can use an aggregation pipeline in update. In MongoDB, the date in unix timestamp is in milliseconds(ms). So, multiply your raw timestamp with 1000 before converting them to date.
For _id 1-4, use $map to perform element-wise transformation.
db.collection.update({
_id: {
$in: [
1,
2,
3,
4
]
}
},
[
{
"$addFields": {
"movies": {
"$map": {
"input": "$movies",
"as": "m",
"in": {
movieid: "$$m.movieid",
rating: "$$m.rating",
timestamp: {
"$toDate": {
"$multiply": [
{
$toLong: "$$m.timestamp"
},
1000
]
}
}
}
}
}
}
}
],
{
multi: true
})
Mongo playground
For _id: 5-8, simply update the field.
db.collection.update({
_id: {
$in: [
5,
6,
7,
8
]
}
},
[
{
"$addFields": {
"timestamp": {
"$toDate": {
"$multiply": [
{
$toLong: "$timestamp"
},
1000
]
}
}
}
}
],
{
multi: true
})
Mongo playground

Express + Mongoose + Aggregation + Calculate Available Stock

I'm using express js and mongoose and i'm new to these platforms. It would be great if someone can help me on this. Please refer the the below data and I'm looking for output like:
itemsizeId: 609578ca23033e55886e7590, AvailableQuantity: 112
itemsizeId: 609578ca23033e55886e758f, AvailableQuantity: 495
Note: Group by movementtype (inward / outward).
Available Stock: inward - outward
[
{
_id: '609fb1a1a7ed990f30d6cae2',
refId: 'Purchase-1',
itemsizeId: '609578ca23033e55886e7590',
itemId: '609578ca23033e55886e758e',
sizeId: '60950c0ba4982390f8dfed79',
movementdate: '2021-05-15T11:33:52.894Z',
movementtype: 'inward',
movementcategory: 'purchase',
quantity: 100,
id: '609fb1a1a7ed990f30d6cae2',
},
{
_id: '609fb1cba7ed990f30d6cae3',
refId: 'Purchase-2',
itemsizeId: '609578ca23033e55886e7590',
itemId: '609578ca23033e55886e758e',
sizeId: '60950c0ba4982390f8dfed79',
movementdate: '2021-05-15T11:34:35.118Z',
movementtype: 'inward',
movementcategory: 'purchase',
quantity: 20,
id: '609fb1cba7ed990f30d6cae3',
},
{
_id: '609fb1fda7ed990f30d6cae4',
refId: 'Sale-1',
itemsizeId: '609578ca23033e55886e7590',
itemId: '609578ca23033e55886e758e',
sizeId: '60950c0ba4982390f8dfed79',
movementdate: '2021-05-15T11:35:25.068Z',
movementtype: 'outward',
movementcategory: 'sales',
quantity: 5,
id: '609fb1fda7ed990f30d6cae4',
},
{
_id: '609fb255a7ed990f30d6cae5',
refId: 'Purchase-3',
itemsizeId: '609578ca23033e55886e758f',
itemId: '609578ca23033e55886e758e',
sizeId: '60950be9a4982390f8dfed78',
movementdate: '2021-05-15T11:36:53.835Z',
movementtype: 'inward',
movementcategory: 'purchase',
quantity: 500,
id: '609fb255a7ed990f30d6cae5',
},
{
_id: '609fb27ea7ed990f30d6cae6',
refId: 'Sale-2',
itemsizeId: '609578ca23033e55886e758f',
itemId: '609578ca23033e55886e758e',
sizeId: '60950be9a4982390f8dfed78',
movementdate: '2021-05-15T11:37:34.066Z',
movementtype: 'outward',
movementcategory: 'sales',
quantity: 8,
id: '609fb27ea7ed990f30d6cae6',
},
]
I have tried till below code and I'm struck to complete it:
const itemStock = await itemStockMovementModel.aggregate([
{
$match: { movementtype: 'inward' },
},
{
$group: {
_id: {
itemsizeId: '$itemsizeId',
},
quantity: { $sum: '$quantity' },
},
},
])
$group by itemsizeId and get quantity sum by condition for inward and outward
$subtract to inward - outward
const itemStock = await itemStockMovementModel.aggregate([
{
$group: {
_id: "$itemsizeId",
inward: {
$sum: { $cond: [{ $eq: ["$movementtype", "inward"] }, "$quantity", 0] }
},
outward: {
$sum: { $cond: [{ $eq: ["$movementtype", "outward"] }, "$quantity", 0] }
}
}
},
{
$project: {
quantity: {
$subtract: ["$inward", "$outward"]
}
}
}
])
Playground
You can use second formula as well, if you don't have third type in movementtype,
$group by itemsizeId
check condition if movementtype is inward then sum quantity otherwise sum negative quantity
const itemStock = await itemStockMovementModel.aggregate([
{
$group: {
_id: "$itemsizeId",
quantity: {
$sum: {
$cond: [
{ $eq: ["$movementtype", "inward"] },
"$quantity",
{ $subtract: [0, "$quantity"] }
]
}
}
}
}
])
Playground