I have the following document structure
{
"_id": "60b7b7c784bd6c2a1ca57f29",
"user": "607c58578bac8c21acfeeae1",
"exercises": [
{
"executed_reps": [8,7],
"_id": "60b7b7c784bd6c2a1ca57f2a",
"exercise_name": "Push up"
},
{
"executed_reps": [5,5],
"_id": "60b7b7c784bd6c2a1ca57f2b",
"exercise_name": "Pull up"
}
],
}
In aggregation, I am trying to sum all the executed_reps so the end value in this example should be 25 (8+7+5+5).
Here is the code I have so far:
const exerciseStats = await UserWorkout.aggregate([
{
$match: {
user: { $eq: ObjectId(req.query.user) },
},
},
{ $unwind: '$exercises' },
{
$group: {
_id: null,
totalReps: {
$sum: {
$reduce: {
input: '$exercises.executed_reps',
initialValue: '',
in: { $add: '$$this' },
},
},
},
},
},
]);
This gives a result of 5 for totalReps. What am I doing wrong?
Well 10 minutes later I found the solution myself:
UserWorkout.aggregate([
{
$match: {
user: { $eq: ObjectId(req.query.user) },
},
},
{ $unwind: '$exercises' },
{
$project: {
total: { $sum: '$exercises.executed_reps' },
},
},
{
$group: {
_id: null,
totalExercises: { $sum: '$total' },
},
},])
Had to use $project first. :)
You can do it like this:
const exerciseStats = await UserWorkoutaggregate([
{
"$addFields": {
"total": {
"$sum": {
"$map": {
"input": "$exercises",
"as": "exercise",
"in": {
"$sum": "$$exercise.executed_reps"
}
}
}
}
}
}
])
Here is the working example: https://mongoplayground.net/p/5_fsPgSh8EP
Related
i am trying to return aggregation result based on user's role and active status
sample doc
{
"email": "doe#gmail.com",
"firstName": "doe",
"lastName": "bghh",
"phone": "+919016703350",
"password":"$.5GIV3q9JqzRqY/lP2",
"status": "verified",
"role": "admin",
"isActive": true
}
const user = await User.aggregate([
{ $project: { isActive: 1, role: 1 } },
{ $group: { _id: { role: '$role', isActive: '$isActive' }, all: { $sum: 1 } } },
])
above query result below
{
"user": [
{
"_id": {
"role": "user",
"isActive": false
},
"all": 1
},
{
"_id": {
"role": "vendor",
"isActive": true
},
"all": 1
},
{
"_id": {
"role": "user",
"isActive": true
},
"all": 2
},
{
"_id": {
"role": "vendor",
"isActive": false
},
"all": 1
},
{
"_id": {
"role": "admin",
"isActive": true
},
"all": 1
}
]
}
expecting result like, help to write query to achieve below result
{ role:{user:{all:3, active:2 }, admin:{all:1, active:1}, vendor:{all:2, active:2}}}
Try this one:
db.collection.aggregate([
{ $project: { isActive: 1, role: 1 } },
{
$group: {
_id: '$role',
all: { $sum: 1 },
active: { $sum: { $cond: ['$isActive', 1, 0] } }
}
},
{
$group: {
_id: null,
role: { $push: "$$ROOT" }
}
},
{
$set: {
role: {
$map: {
input: "$role",
in: { k: "$$this._id", v: { all: "$$this.all", active: "$$this.active" } }
}
}
}
},
{ $set: { role: { $arrayToObject: "$role" } } },
{ $replaceWith: { role: "$role" } }
])
Mongo Playground
I have a collection named Vote that looks like the following:
{
postId: "1",
comment:{
text_sentiment: "positive",
topic: "A"
}
}, // DOC-1
{
postId: "2",
comment:{
text_sentiment: "negative",
topic: "A"
}
}, // DOC-2
{
postId: "3",
comment:{
text_sentiment: "positive",
topic: "B"
}
},..//DOC-3 ..
and a collection named post which looks as follows?
{
_id: "1",
category: "a"
},
{
_id: "2",
category: "b",
}
I want to do an aggregation on this collection such that it returns the following structure. (which also takes into account the other post collection`
[
{
_id: "hash",
topic: "A",
topicOccurance: 2,
sentiment: {
positive: 1,
negative: 1,
neutral: 0
},
postIds: [1,2],
categories: [a,b]
},
..
]
I created the following aggregation:
db.Vote.aggregate([
{
$match: {
surveyId: "e6d38e1ecd",
"comment.topic": {
$exists: 1
},
}
},
{
$group: {
_id: {
topic: "$comment.topic",
text_sentiment: "$comment.text_sentiment"
},
total: {
$sum: 1
},
postIds: {
$push: "$postId"
}
}
},
{
$group: {
_id: "$_id.topic",
total: {
$sum: "$total"
},
text_sentiments: {
$push: {
k: "$_id.text_sentiment",
v: "$total"
}
},
postIds: {
"$push": "$postIds"
}
}
},
{
$project: {
topic: "$_id",
topicOccurance: "$total",
sentiment: {
"$arrayToObject": "$text_sentiments"
},
postIds: {
$reduce: {
input: "$postIds",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this"
]
}
}
}
}
},
{
$sort: {
"topicOccurance": -1
}
}
])
This works fine but I do not know, how to also get categories which exist in a separate collection named posts. How can I query a different collection while performing aggregation?
A simple $lookup will suffice:
db.Vote.aggregate([
{
$match: {
"comment.topic": {
$exists: 1
},
}
},
{
$group: {
_id: {
topic: "$comment.topic",
text_sentiment: "$comment.text_sentiment"
},
total: {
$sum: 1
},
postIds: {
$push: "$postId"
}
}
},
{
$group: {
_id: "$_id.topic",
total: {
$sum: "$total"
},
text_sentiments: {
$push: {
k: "$_id.text_sentiment",
v: "$total"
}
},
postIds: {
"$push": "$postIds"
}
}
},
{
$project: {
topic: "$_id",
topicOccurance: "$total",
sentiment: {
"$arrayToObject": "$text_sentiments"
},
postIds: {
$reduce: {
input: "$postIds",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this"
]
}
}
}
}
},
{
$sort: {
"topicOccurance": -1
}
},
{
$lookup: {
from: "post",
localField: "postIds",
foreignField: "_id",
as: "categories"
}
},
{
$addFields: {
categories: {
"$setUnion": {
$map: {
input: "$categories",
in: "$$this.category"
}
}
}
}
}
])
Mongo Playground
Expected Output:
[
{
"_id": "healty",
"doc_count": 2,
"ingredients": {
"Leaves": {
"1.2g": 1,
"1.5g": 1
},
"Spinach": {
"12g": 1,
"18g": 1
}
}
},
{
"_id": "junk",
"doc_count": 3,
"ingredients": {
"cheese": {
"100g": 1,
"120g": 2
},
"meat": {
"50g": 1,
"60g": 1,
"70g": 1
}
}
}
]
Aggregation Below: Playground1
db.collection.aggregate([
{
$group: {
"_id": "$type", // grouping the document by "type" field
"ingredients": {
$push: "$$ROOT" //want to run Playground2 aggrgtion in each of it
},
"doc_count": {
$sum: 1 // total count
}
}
}
])
After that,
I've also figured out an Another Step to convert the ingredients array after it: Playground2
But, this aggregation is for All the documents. I want to make Playground2 aggregation work for the ingredients field only in each group Object.
It's similar to combining Playground1 & Playground2. How do I do this?
db.collection.aggregate([
{
"$set": {
"ingredients": {
"$objectToArray": "$ingredients"
}
}
},
{
"$unwind": "$ingredients"
},
{
"$group": {
"_id": {
type: "$type",
ingredient: "$ingredients"
},
"count": {
"$sum": 1
},
"doc_count": {
"$addToSet": "$item"
}
}
},
{
"$group": {
"_id": {
type: "$_id.type",
ingredient: "$_id.ingredient.k"
},
"docs": {
"$push": {
k: "$_id.ingredient.v",
v: "$count"
}
},
"doc_count": {
"$push": "$doc_count"
}
}
},
{
"$group": {
"_id": "$_id.type",
"ingredients": {
"$push": {
k: "$_id.ingredient",
v: {
"$arrayToObject": "$docs"
}
}
},
"doc_count": {
"$push": {
$reduce: {
input: "$doc_count",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this"
]
}
}
}
}
}
},
{
"$set": {
"ingredients": {
"$arrayToObject": "$ingredients"
},
doc_count: {
"$size": {
"$setUnion": {
$reduce: {
input: "$doc_count",
initialValue: [],
in: {
$concatArrays: [
"$$value",
"$$this"
]
}
}
}
}
}
}
}
])
mongoplayground
I'm trying to get data from 2 collections, and return one array with merge data of both collection.
The best solution for me was :
const bothValues = await ValueA.aggregate([
{ $unionWith: { coll: 'valueB' } },
{ $sort: { rank: -1, _id: -1 } },
{
$match: {
isAvailable: true,
},
},
{ $skip: skip },
{ $limit: 30 },
]);
which work perfectly. But.. $unionWith was not implemented my MongoDB version (4.0.X) so I can't use it.
const bothValues = await ValueA.aggregate(
[
{ $limit: 1 },
{
$lookup: {
from: 'valueB',
pipeline: [{ $limit: 15 }],
as: 'valueB',
},
},
{
$lookup: {
from: 'ValueA',
pipeline: [{ $limit: 15 }, { $sort: { rank: -1, _id: -1 } }],
as: 'ValueA',
},
},
{
$project:
{
Union: { $concatArrays: ['$valueB', '$ValueA'] },
},
},
{ $unwind: '$Union' },
{ $replaceRoot: { newRoot: '$Union' } },
],
);
but now, I got 2 problems :
I can't use a $skip, which is important, where use it ?
How to use $match ?
Thanks
Query
your query made with some changes to work like the first query
match in both pipelines, sort in both, (limit limitN+skipN)
(this way we make sure that we always have enough documents even if all are taken from valueA or valueB)
Take sorted 70 from each, so in all ways we will have the 70 needed in the final sort/skip/limit after the union.
concat,unwind,replace-root like in your query
sort again (to sort the union now), skip, limit
no matter we always have enough documents to skip
this example query is made for skip=40 and limit=30 so in the first 2 pipelines we limit=70
db.ValueA.aggregate([
{
"$limit": 1
},
{
"$lookup": {
"from": "valueB",
"pipeline": [
{
"$match": {
"isAvailable": true
}
},
{
"$sort": {
"rank": -1,
"_id": -1
}
},
{
"$limit": 70
}
],
"as": "valueB"
}
},
{
"$lookup": {
"from": "valueA",
"pipeline": [
{
"$match": {
"isAvailable": true
}
},
{
"$sort": {
"rank": -1,
"_id": -1
}
},
{
"$limit": 70
}
],
"as": "valueA"
}
},
{
"$project": {
"union": {
"$concatArrays": [
"$valueA",
"$valueB"
]
}
}
},
{
"$unwind": {
"path": "$union"
}
},
{
"$replaceRoot": {
"newRoot": "$union"
}
},
{
"$sort": {
"rank": -1,
"_id": -1
}
},
{
"$skip": 40
},
{
"$limit": 30
}
])
I have this data structure:
{
"_id": "5ebd08794bcc8d2fd893f4a7",
"username": "johan#gmail.com",
"password": "123",
"decks": [{
"cards": [{
"_id": "5ebd08794bcc8d2fd893f4a9",
"planeetnaam": "Venus",
"kleur": "Grijs"
},
{
"_id": "5ebd08794bcc8d2fd893f4aa",
"planeetnaam": "Neptunus",
"kleur": "Paars"
}
],
"_id": "5ebd08794bcc8d2fd893f4a8",
"name": "Planeten"
},
{
"cards": [{
"_id": "5ebd08794bcc8d2fd893f4ac",
"diernaam": "Hond",
"poten": "4"
},
{
"_id": "5ebd08794bcc8d2fd893f4ad",
"diernaam": "Kangoeroe",
"poten": "2"
}
],
"_id": "5ebd08794bcc8d2fd893f4ab",
"name": "Dieren"
}
],
"__v": 0
}
Now i want to add a new property to all the cards in deck with deckname: "Planeten". How do i do this with a mongoose query?
The cards array of deck "Planeten" should look like this after the query
"cards": [{
"_id": "5ebd08794bcc8d2fd893f4a9",
"planeetnaam": "Venus",
"kleur": "Grijs",
"newProp": null
},
{
"_id": "5ebd08794bcc8d2fd893f4aa",
"planeetnaam": "Neptunus",
"kleur": "Paars",
"newProp": null
}
],
EDIT:
This works in Robo3T:
db.getCollection('users').findOneAndUpdate(
{ '_id': ObjectId("5eba9ee0abfaf237f81fb104") },
{ $set: { 'decks.$[deck].cards.$[].newProp': null } },
{ arrayFilters: [{ 'deck._id': ObjectId("5eba9ee0abfaf237f81fb108") } ] }
)
But the server query doesnt edit any data:
User.findOneAndUpdate(
{ '_id': req.session.userid },
{ $set: { 'decks.$[deck].cards.$[].newProp': null } },
{ arrayFilters: [{ 'deck._id': req.params.deckid } ] }, function(err, user){
res.send('test');
})
Thanks in advance
you can use array update operators
the query may look something like that
db.collection.updateOne(
{ _id: <ObjectId> }, // the filter part
{ $set: { 'decks.$[deck].cards.$[].newProp': null } },
{ arrayFilters: [{ 'deck.name': 'Planeten' }] }
)
$[deck] refers to each element in the decks array
$[] is used to update all the elements in the cards array
your function may look something like that
User.updateOne(
{ '_id': req.session.userid },
{ $set: { 'decks.$[deck].cards.$[].newProp': null } },
{ arrayFilters: [{ 'deck.name': 'Planeten' }] })
.then(function (user) {
if (!user) {
res.status(404).send('Er ging helaas iets fout')
} else {
res.status(201).send("Card is toegevoegd");
}
})
hope it helps