0

I have a collection in MongoDB, where my data looks like below. I am using NodeJS 12.X platform along with Mongoose at backend for my application . Since i am new to development background, kindly help me in it.

Collection Data in DB :

{
        "_id" : ObjectId("603faf38150786680421b51f"),
        "conversationMetricName" : [
                {
                        "metric" : "nConnected",
                        "stats" : {
                                "count" : 3
                        }
                },
                {
                        "metric" : "tAbandon",
                        "stats" : {
                                "max" : 2779,
                                "min" : 2779,
                                "count" : 1,
                                "sum" : 2779
                        }
                },
                {
                        "metric" : "tAcd",
                        "stats" : {
                                "max" : 2779,
                                "min" : 2779,
                                "count" : 1,
                                "sum" : 2779
                        }
                }
        ],
        "conversationInterval" : "2021-01-18T18:30:00.000Z/2021-01-19T18:30:00.000Z",
        "__v" : 0
}
{
        "_id" : ObjectId("603faf38150786680421b520"),
        "conversationMetricName" : [
                {
                        "metric" : "nConnected",
                        "stats" : {
                                "count" : 19
                        }
                },
                {
                        "metric" : "tAbandon",
                        "stats" : {
                                "max" : 293745,
                                "min" : 214878,
                                "count" : 2,
                                "sum" : 508623
                        }
                },
                
        ],
        "conversationInterval" : "2021-01-19T18:30:00.000Z/2021-01-20T18:30:00.000Z",
        "__v" : 0
}
}

Expected Response :

{
chart:{

   date: [2021-01-18T18:30:00.000Z/2021-01-19T18:30:00.000Z, 2021-01-19T18:30:00.000Z/2021-01-20T18:30:00.000Z],
   nConnected: [3,19],
   tAbandon: [1,2],
   tAcd: [1,0],
   
   },

}

Could anyone advise me , how to filter the data as like above using their Counts value. Thanks.

1 Answer 1

1

Use the sort method

collection.sort( { count: 1 } )

https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort

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

1 Comment

Hi @n7f , Actually i am trying to retrieve the desired response as posted above . Here i am not doing the sorting functionality. I can able to get the dates value, but remaining responses i am not sure how to retrieve count value inside the array in respect with dates.

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.