Getting two different values into different arrays

I’m having a data in my collection as following,

{
    _Id:objectId()
    CreatedOn:IsoDate(2022-05-12)
    PowerAvailiabilityTime:IsoDate(2022-05-12)
    stat:0
}
{
    _Id:objectId()
    CreatedOn:IsoDate(2022-05-12)
    PowerAvailiabilityTime:IsoDate(2022-05-12)
    stat:1
}

Now I want to get all stat:0 records in to one array and stat:1 records into one array, by using aggregation, kindly help me in this.

Please provide more sample documents, you shared only one of each case so it is not much for us to experiment with.

Also share the expected results.

Thanks for replying Steevej, As you said following is the sample data

/* 1 */
{
    "_id" : ObjectId("61f3afa32bf8817538c898a1"),
    "stat" : 0,
    "time" : NumberLong(1643359570),
    "powerAvailabilityTime" : ISODate("2022-01-28T08:46:10.000Z"),
    "temperatureLogId" : "61f3afa32",
    "dId" : "356849088",
    "month" : "Jan",
    "year" : "2022",
    "powerStringDate" : "2022-01-28",
    "storeId" : NumberLong(0),
    "storeName" : "",
    "stateId" : 0,
    "districtId" : 0,
    "blockId" : 0,
    "city" : "",
    "badgeName" : "",
    "updatedOn" : ISODate("2022-01-28T08:56:03.022Z"),
    "createdOn" : ISODate("2022-01-28T08:56:03.022Z"),
    "isDeleted" : false,
    
}

/* 2 */
{
    "_id" : ObjectId("61f3c9a22bf8817538c89b06"),
    "stat" : 1,
    "time" : NumberLong(1643366213),
    "powerAvailabilityTime" : ISODate("2022-01-28T10:36:53.000Z"),
    "temperatureLogId" : "61f3c9a22b",
    "dId" : "356849088",
    "month" : "Jan",
    "year" : "2022",
    "powerStringDate" : "2022-01-28",
    "storeId" : NumberLong(0),
    "storeName" : "",
    "stateId" : 0,
    "districtId" : 0,
    "blockId" : 0,
    "city" : "",
    "badgeName" : "",
    "updatedOn" : ISODate("2022-01-28T10:46:58.924Z"),
    "createdOn" : ISODate("2022-01-28T10:46:58.924Z"),
    "isDeleted" : false,
    
}

/* 3 */
{
    "_id" : ObjectId("61fa749abe414854f990aae4"),
    "stat" : 0,
    "time" : NumberLong(1643803135),
    "powerAvailabilityTime" : ISODate("2022-02-02T11:58:55.000Z"),
    "temperatureLogId" : "61fa749ab",
    "assetDId" : "201100027MR",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-02",
    "storeId" : NumberLong(1),
    "storeName" : "Agra DVS",
    "stateId" : 573,
    "districtId" : 1802,
    "blockId" : 0,
    "city" : "agra",
    "badgeName" : "DVS",
    "updatedOn" : ISODate("2022-02-02T12:10:02.439Z"),
    "createdOn" : ISODate("2022-02-02T12:10:02.439Z"),
    "isDeleted" : false,
    
}

/* 4 */
{
    "_id" : ObjectId("61fb7fee7eab2d12568a6d50"),
    "stat" : 1,
    "time" : NumberLong(1643871511),
    "powerAvailabilityTime" : ISODate("2022-02-03T06:58:31.000Z"),
    "temperatureLogId" : "61fb7fed7eab",
    "assetDId" : "201100027MR",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-03",
    "storeId" : NumberLong(2),
    "storeName" : "Agra DVS",
    "stateId" : 573,
    "districtId" : 1802,
    "blockId" : 0,
    "city" : "agra",
    "badgeName" : "DVS",
    "updatedOn" : ISODate("2022-02-03T07:10:38.156Z"),
    "createdOn" : ISODate("2022-02-03T07:10:38.156Z"),
    "isDeleted" : false,
    
}

/* 5 */
{
    "_id" : ObjectId("61fcc7377eab2d12568a9b0a"),
    "stat" : 0,
    "time" : NumberLong(1643955409),
    "powerAvailabilityTime" : ISODate("2022-02-04T06:16:49.000Z"),
    "temperatureLogId" : "61fcc7377eab2d",
    "assetDId" : "201100027MR",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-04",
    "storeId" : NumberLong(3),
    "storeName" : "Agra DVS",
    "stateId" : 573,
    "districtId" : 1802,
    "blockId" : 0,
    "city" : "agra",
    "badgeName" : "DVS",
    "updatedOn" : ISODate("2022-02-04T06:27:03.592Z"),
    "createdOn" : ISODate("2022-02-04T06:27:03.592Z"),
    "isDeleted" : false,
   
}

/* 6 */
{
    "_id" : ObjectId("61fd2d2c7da7233dd1e91979"),
    "stat" : 0,
    "time" : NumberLong(1643981587),
    "powerAvailabilityTime" : ISODate("2022-02-04T13:33:07.000Z"),
    "temperatureLogId" : "61fd2d2c7da7233dd",
    "assetDId" : "201100027MR",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-04",
    "storeId" : NumberLong(4),
    "storeName" : "Agra DVS",
    "stateId" : 573,
    "districtId" : 1802,
    "blockId" : 0,
    "city" : "agra",
    "badgeName" : "DVS",
    "updatedOn" : ISODate("2022-02-04T13:42:04.610Z"),
    "createdOn" : ISODate("2022-02-04T13:42:04.610Z"),
    "isDeleted" : false,
    
}

/* 7 */
{
    "_id" : ObjectId("62025c822733a70dc9d10ff8"),
    "stat" : 0,
    "time" : NumberLong(1644321229),
    "powerAvailabilityTime" : ISODate("2022-02-08T11:53:49.000Z"),
    "temperatureLogId" : "62025c822733a",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-08",
    "storeId" : NumberLong(0),
    "storeName" : "",
    "stateId" : 0,
    "districtId" : 0,
    "blockId" : 0,
    "city" : "",
    "badgeName" : "",
    "updatedOn" : ISODate("2022-02-08T12:05:22.684Z"),
    "createdOn" : ISODate("2022-02-08T12:05:22.684Z"),
    "isDeleted" : false,
   
}

/* 8 */
{
    "_id" : ObjectId("62027d722733a70dc9d1138e"),
    "stat" : 1,
    "time" : NumberLong(1644329658),
    "powerAvailabilityTime" : ISODate("2022-02-08T14:14:18.000Z"),
    "temperatureLogId" : "62027d722733a70",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-08",
    "storeId" : NumberLong(0),
    "storeName" : "",
    "stateId" : 0,
    "districtId" : 0,
    "blockId" : 0,
    "city" : "",
    "badgeName" : "",
    "updatedOn" : ISODate("2022-02-08T14:25:54.915Z"),
    "createdOn" : ISODate("2022-02-08T14:25:54.915Z"),
    "isDeleted" : false,
    
}

/* 9 */
{
    "_id" : ObjectId("6203711c0b90c148626575ea"),
    "stat" : 0,
    "time" : NumberLong(1644392090),
    "powerAvailabilityTime" : ISODate("2022-02-09T07:34:50.000Z"),
    "temperatureLogId" : "6203711c0b90c14",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-09",
    "storeId" : NumberLong(0),
    "storeName" : "",
    "stateId" : 0,
    "districtId" : 0,
    "blockId" : 0,
    "city" : "",
    "badgeName" : "",
    "updatedOn" : ISODate("2022-02-09T07:45:32.508Z"),
    "createdOn" : ISODate("2022-02-09T07:45:32.508Z"),
    "isDeleted" : false,
    
}

/* 10 */
{
    "_id" : ObjectId("62039b010b90c148626578e5"),
    "stat" : 1,
    "time" : NumberLong(1644402766),
    "powerAvailabilityTime" : ISODate("2022-02-09T10:32:46.000Z"),
    "temperatureLogId" : "62039b010b90c",
    "dId" : "356849088",
    "month" : "Feb",
    "year" : "2022",
    "powerStringDate" : "2022-02-09",
    "storeId" : NumberLong(6),
    "storeName" : "Agra DH",
    "stateId" : 573,
    "districtId" : 1802,
    "blockId" : 0,
    "city" : "District Hospital",
    "badgeName" : "CCP",
    "updatedOn" : ISODate("2022-02-09T10:44:17.715Z"),
    "createdOn" : ISODate("2022-02-09T10:44:17.715Z"),
    "isDeleted" : false,
    
}

Currently im using 4.4 version in my environment, im expecting all the records which contains “STAT:0” should come into one array, and the records which contails “STAT:1” should come into another seperate array Is it possible to split into two different arrays. I hope you get my concen.

Please read Formatting code and log snippets in posts and update your documents so that we can cut-n-paste into our system.

You need aggregation.

A $match stage to keep only stat:0 and stat:1 documents.

Then a $group stage that specifies _id:$stat and includes a $push to build the array.

Thank you Steevej, with your suggestions i am able to get the results as per my requirement. Thanks Again

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.