Home > Software engineering >  Not able to make the single array of objects with two different arrays for making the start and end
Not able to make the single array of objects with two different arrays for making the start and end

Time:09-27

I have a dataset like this:

[
    {
        "_id" : ObjectId("6332c4d8a813124264860f83"),
        "prefixStr" : "ABCD",
        "startRange" : 1,
        "endRange" : 5,
        "createdate" : ISODate("2022-09-27T09:39:36.319Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4d8a813124264860f84"),
        "prefixStr" : "ABCD",
        "startRange" : 1,
        "endRange" : 5,
        "createdate" : ISODate("2022-09-27T09:39:36.319Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4d8a813124264860f85"),
        "prefixStr" : "ABCD",
        "startRange" : 1,
        "endRange" : 5,
        "createdate" : ISODate("2022-09-27T09:39:36.319Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4d8a813124264860f86"),
        "prefixStr" : "ABCD",
        "startRange" : 1,
        "endRange" : 5,
        "createdate" : ISODate("2022-09-27T09:39:36.320Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4d8a813124264860f87"),
        "prefixStr" : "ABCD",
        "startRange" : 1,
        "endRange" : 5,
        "createdate" : ISODate("2022-09-27T09:39:36.320Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4e1a813124264860f88"),
        "prefixStr" : "ABCD",
        "startRange" : 11,
        "endRange" : 15,
        "createdate" : ISODate("2022-09-27T09:39:45.739Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4e1a813124264860f89"),
        "prefixStr" : "ABCD",
        "startRange" : 11,
        "endRange" : 15,
        "createdate" : ISODate("2022-09-27T09:39:45.743Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4e1a813124264860f8a"),
        "prefixStr" : "ABCD",
        "startRange" : 11,
        "endRange" : 15,
        "createdate" : ISODate("2022-09-27T09:39:45.746Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4e1a813124264860f8b"),
        "prefixStr" : "ABCD",
        "startRange" : 11,
        "endRange" : 15,
        "createdate" : ISODate("2022-09-27T09:39:45.747Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4e1a813124264860f8c"),
        "prefixStr" : "ABCD",
        "startRange" : 11,
        "endRange" : 15,
        "createdate" : ISODate("2022-09-27T09:39:45.748Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4eaa813124264860f8d"),
        "prefixStr" : "ABCD",
        "startRange" : 21,
        "endRange" : 25,
        "createdate" : ISODate("2022-09-27T09:39:54.203Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4eaa813124264860f8e"),
        "prefixStr" : "ABCD",
        "startRange" : 21,
        "endRange" : 25,
        "createdate" : ISODate("2022-09-27T09:39:54.204Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4eaa813124264860f8f"),
        "prefixStr" : "ABCD",
        "startRange" : 21,
        "endRange" : 25,
        "createdate" : ISODate("2022-09-27T09:39:54.205Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4eaa813124264860f90"),
        "prefixStr" : "ABCD",
        "startRange" : 21,
        "endRange" : 25,
        "createdate" : ISODate("2022-09-27T09:39:54.207Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c4eaa813124264860f91"),
        "prefixStr" : "ABCD",
        "startRange" : 21,
        "endRange" : 25,
        "createdate" : ISODate("2022-09-27T09:39:54.208Z"),
        "__v" : 0
    },
    {
        "_id" : ObjectId("6332c623edb18941f8aeabea"),
        "prefixStr" : "ANKT001",
        "startRange" : 0,
        "endRange" : 0,
        "createdate" : ISODate("2022-09-27T09:45:07.570Z"),
        "__v" : 0
    }
]

Now I want to aggregate the collection so that result will be like this:

Expected results

[
  { _id: 'ANKT001', range: [ {start: 0, end: 0 }]},
  { _id: 'ABCD', range: [ {start: 1, end: 5 }, {start: 11, end: 15 }, {start: 21, end: 25 } }
]

This is my code by which I am getting the result but not as expectation which was mentioned above:

My current query result

[
  { _id: 'ANKT001', startRange: [ 0 ], endRange: [ 0 ] },
  { _id: 'ABCD', startRange: [ 11, 21, 1 ], endRange: [ 5, 15, 25 ] }
]

This is My Query:

const getUsedRanges = await MyCode.aggregate(
                [
                    { 
                        $group: { 
                            _id: "$prefixStr",
                            startRange: { $addToSet: "$startRange"},
                            endRange: { $addToSet: "$endRange" },
                        }
                    },
                    {
                        $project : { 
                            _id: 1,
                            startRange: 1,
                            endRange: 1,
                        }
                    }
                ]
            ).exec();
            console.log(getUsedRanges);
            if (getUsedRanges) {
                let resp = getUsedRanges.map(x => {
                    return {
                        prefix: x._id,
                        startRange: x.startRange.sort((a, b) => {
                            return a - b;
                        }),
                        endRange: x.endRange.sort((a, b) => {
                            return a - b;
                        })
                    }
                });
                console.log(resp);
            }

Can Anyone guide me how to make the query to get the response in array of objects for range array

CodePudding user response:

You have to create range property and pass start and end propeties inside $addToSet operator,

const getUsedRanges = await MyCode.aggregate(
  {
    $group: {
      _id: "$prefixStr",
      range: {
        $addToSet: {
          start: "$startRange",
          end: "$endRange"
        }
      }
    }
  }
]).exec();

Playground

  • Related