mongo db aggregation how to get all fields - javascript

I am trying to write the aggregation total average duration.
I got the output but not coming all fields.
How to get all fields in the results?
Any one please suggest me.
db.lights.aggregate(
{
$match: {
CREATE_DATE: {
$gte: ISODate("2018-01-24T20:05:30.000Z"),
$lt: ISODate("2018-02-24T20:05:30.000Z")
}
}
},{ $addFields: {
offduration: {
$divide: [
{ $subtract: ["$RECEIVEDDATE", "$CREATE_DATE"] },
3600000
]
}
}
}, { "$group": {
_id: {
SWITCHID: "$SWITCHID",
STATUS: "$STATUS"
},
avgduration: { $avg: "$offduration" },
SWITCHID: { $first: "$SWITCHID" },
CREATE_DATE: { $first: "$CREATE_DATE" },
RECEIVEDDATE: { $first: "$RECEIVEDDATE" },
STATUS: { $first: "$STATUS" },
offduration: { $first: "$offduration" },
} },
{ $project: {
_id: 0,
SWITCHID: 1,
CREATE_DATE: 1,
RECEIVEDDATE: 1,
STATUS: 1,
avgduration: '$avgduration',
offduration: '$offduration'
} },
{"$group" : {
_id: { SWITCHID: "$SWITCHID" },
on_minus_off: {
$sum:{ "$cond": [
{ "$eq": ["$STATUS", "LIGHTS OFF"] },
"$avgduration",
{ $subtract: [ 0, "$avgduration" ] }
]
}
}
}
}
)

You want to add "$project" for the required field in the first pipeline. then you want to apply in $group pipeline like this,
$project: {
requireFiled1 : "$requireFiled1",
requireFiled2 : "$requireFiled2", }
// And add the below in $group
requiredField1 : {
$first: "$requiredField1" // Projected Name in Projection Section
},
db.lights.aggregate({
$match: {
CREATE_DATE: {
$gte: ISODate("2018-01-24T20:05:30.000Z"),
$lt: ISODate("2018-02-24T20:05:30.000Z")
}
}
},
{
$addFields: {
offduration: {
$divide: [
{
$subtract: [
"$RECEIVEDDATE",
"$CREATE_DATE"
]
},
3600000
]
}
}
},
{
"$group": {
_id: {
SWITCHID: "$SWITCHID",
STATUS: "$STATUS"
},
avgduration: {
$avg: "$offduration"
},
SWITCHID: {
$first: "$SWITCHID"
},
CREATE_DATE: {
$first: "$CREATE_DATE"
},
RECEIVEDDATE: {
$first: "$RECEIVEDDATE"
},
STATUS: {
$first: "$STATUS"
},
offduration: {
$first: "$offduration"
},
}
},
{
$project: {
_id: 0,
SWITCHID: 1,
CREATE_DATE: 1,
RECEIVEDDATE: 1,
STATUS: 1,
avgduration: '$avgduration',
offduration: '$offduration'
}
},
{
"$group": {
_id: {
SWITCHID: "$SWITCHID"
},
requireFiled1 : {
$first: "$requireFiled1"
},
on_minus_off: {
$sum: {
"$cond": [
{
"$eq": [
"$STATUS",
"LIGHTS OFF"
]
},
"$avgduration",
{
$subtract: [
0,
"$avgduration"
]
}
]
}
}
}
})

Related

Getting data from a different collection while performing aggregation on a different collection

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

Mongodb: How to pass another stage inside $group?

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

MongoDB Aggregate sum nested number array

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

Getting error, 'The field name '$acknowledged' cannot be an operator name' with aggregation query

I'm trying to fetch all latest messages between User A and any other user.
I keep running into the error ,
The field name '$acknowledged' cannot be an operator name
Not sure what I'm doing wrong here. Mongo playground.
The expected output should be the latest message exchanged between user with id 5a934e000102030405000001, and any other user.
[
{
"from": ObjectId("5a934e000102030405000002"),
"to": ObjectId("5a934e000102030405000001"),
"acknowledged": true,
date: "2020-04-17T18:26:34.353+00:00"
},
{
"from": ObjectId("5a934e000102030405000001"),
"to": ObjectId("5a934e000102030405000003"),
"acknowledged": false,
date: "2020-04-17T18:26:31.353+00:00"
},
{
"from": ObjectId("5a934e000102030405000004"),
"to": ObjectId("5a934e000102030405000001"),
"acknowledged": false,
date: "2020-04-17T18:26:29.353+00:00"
},
]
You had a typo here:
$acknowledged: { acknowledged: {
$first: "$acknowledged", --> $first: "$acknowledged"
}
},
and
then: "$responseTo", --> then: "$to",
db.Message.aggregate([
{
$match: {
$or: [
{
from: {
$in: [
ObjectId("5a934e000102030405000001")
]
}
},
{
to: {
$in: [
ObjectId("5a934e000102030405000001")
]
}
}
]
}
},
{
$sort: {
date: -1
}
},
{
$group: {
_id: {
userConcerned: {
$cond: [
{
$in: [
"$to",
[
ObjectId("5a934e000102030405000001")
]
]
},
"$to",
"$from"
]
},
interlocutor: {
$cond: [
{
$in: [
"$to",
[
ObjectId("5a934e000102030405000001")
]
]
},
"$from",
"$to"
]
}
},
id: {
$first: "$_id"
},
from: {
$first: "$from"
},
acknowledged: {
$first: "$acknowledged"
},
to: {
$first: "$to"
},
date: {
$first: "$date"
}
}
},
{
$lookup: {
from: "User",
localField: "to",
foreignField: "_id",
as: "to"
}
},
{
$unwind: "$to"
},
{
$lookup: {
from: "User",
localField: "from",
foreignField: "_id",
as: "from"
}
},
{
$unwind: "$from"
},
{
$project: {
_id: 0,
date: 1,
acknowledged: 1,
from: "$from._id",
to: "$to._id"
}
}
])
MongoPlayground

MongoDB group aggregation with condition in $sum

Collection of my database is something like below
[{
_id:1,
status:"active",
sale: 4,
createdAt:"2019-10-08 08:46:19"
},
{
_id:2,
status:"inactive",
sale:5,
createdAt:"2019-10-08 06:41:19"
},
{
_id:2,
status:"inactive",
sale:5,
createdAt:"2019-10-08 02:01:19"
}]
I need to group it by "day".The result which I want
[{
createdAt:"2019-10-08 02:01:19",
inactive: 2,
active:1,
salesOfActive: 4,
salesOfInactive:10
}]
I am not getting the actual result which I want any help will be highly appreciated
I had try with this but won't get an idea how i will get salesOfActive and salesOfInactive per day
{
$group: {
_id: {
day: { $dayOfMonth: "$createdAt" }
},
inActive:{$sum: { status:"inactive"}},
active:{$sum: { status:"active"}},
salesOfActive: { $sum:$sale }
}
}
Basically you need to $sum each field $conditionally here
db.collection.aggregate([
{ "$group": {
"_id": {
"$dayOfMonth": { "$dateFromString": { "dateString": "$createdAt" } }
},
"inactive": {
"$sum": { "$cond": [{ "$eq": ["$status", "inactive"] }, 1, 0] }
},
"active": {
"$sum": { "$cond": [{ "$eq": ["$status", "inactive"] }, 0, 1] }
},
"salesOfInactive": {
"$sum": { "$cond": [{ "$eq": ["$status", "inactive"] }, "$sale", 0] }
},
"salesOfActive": {
"$sum": { "$cond": [{ "$eq": ["$status", "inactive"] }, 0, "$sale"] }
}
}}
])
MongoPlayground

Categories