0

I am new to MongoDB, here i am trying to transforming the data inside each documents with the given data. Please help me to complete this task.

getconvaggregate1 collection:(existing)

 {
            "_id" : ObjectId("603faf38150786680421b52b"),
            "conversationMetricName" : [
                    {
                            "metric" : "nConnected",
                            "stats" : {
                                    "count" : 3
                            }
                    },
                    {
                            "metric" : "tAbandon",
                            "stats" : {
                                    "max" : 319012,
                                    "min" : 7372,
                                    "count" : 1,
                                    "sum" : 326384
                            }
                    },
                    {
                            "metric" : "tAcd",
                            "stats" : {
                                    "max" : 319012,
                                    "min" : 7372,
                                    "count" : 1,
                                    "sum" : 326384
                            }
                    }
            ],
            "conversationInterval" : "2021-02-17T18:30:00.000Z/2021-02-18T18:30:00.000Z",
            "__v" : 0
    }
{
        "_id" : ObjectId("603faf38150786680421b52b"),
        "conversationMetricName" : [
                {
                        "metric" : "nConnected",
                        "stats" : {
                                "count" : 19
                        }
                },
                {
                        "metric" : "tAbandon",
                        "stats" : {
                                "max" : 319012,
                                "min" : 7372,
                                "count" : 2,
                                "sum" : 326384
                        }
                }
                
        ],
        "conversationInterval" : "2021-02-18T18:30:00.000Z/2021-02-19T18:30:00.000Z",
        "__v" : 0
}

Required Collection Data :

Here i need to compare with value of "metric" and need to pick the "Count" correspondingly. If suppose if certain metric not present , i need to map that with '0' value.

{
        "_id" : ObjectId("60468a01010e8f08b0f794d6"),
        "conversationInterval" : "2021-02-17T18:30:00.000Z/2021-02-18T18:30:00.000Z",
        "nConnected":3
        "tAbandon":1,
        "tAcd":1
        "__v" : 0
}      
{
        "_id" : ObjectId("60468a01010e8f08b0f794d7"),
        "conversationInterval" : "2021-02-18T18:30:00.000Z/2021-02-19T18:30:00.000Z",
        "nConnected":19
        "tAbandon":2,
        "tAcd":0
        "__v" : 0
}

1 Answer 1

0

Here is a solution with an aggregate using $arrayToObject and $replaceRoot

db.collection.aggregate([
  {
    "$project": {
      "_id": 1,
      "__v": 1,
      "conversationInterval": 1,
      "metrics": {
        "$arrayToObject": {
          "$map": {
            "input": "$conversationMetricName",
            "as": "el",
            "in": {
              "k": "$$el.metric",
              "v": "$$el.stats.count"
            }
          }
        }
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$mergeObjects": [
          "$metrics",
          "$$ROOT"
        ]
      }
    }
  },
  {
    "$project": {
      "metrics": 0
    }
  }
])

Try it here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your help. I have only one suggestion required , if suppose the metric -tAcd need to be shown as 0 , currently "nConnected":19,"tAbandon":2, but missing "tAcd":0. Is it possible to show the missing values as 0 , according to the data present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.