Mongodb move object conditionaly

I was trying to make an attendance application. I am using Mongodb and koajs for development. There is night shift option and I am unable to handle when the attendance date is changing. I need to move next dated first record to current dated record when the current day’s shift is night shift. My current data set look like bellow.

[
    {
        "date": "2023-04-01",
        "shift": {
            "_id": "6422d103994726677e9105c3",
            "date": "2023-04-01",
            "shiftType": {
                "_id": "6299fd7504978a2ba513e0a2",
                "name": "Next Day End",
                "isNight": true
            }
        },
        "attendances": [
            {
                "_id": "6422d1b9994726677e9105c6",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-01T05:30:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-02",
        "shift": null, //Shift not yet set, treated as general shift.
        "attendances": [
            {
                "_id": "6422d1b9994726677e9105c7",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-02T00:30:00.000Z"
            },
            {
                "_id": "6423286e2746f65c2480a13e",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-02T04:30:00.000Z"
            },
            {
                "_id": "6423286e2746f65c2480a13f",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-02T12:40:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-03",
        "shiftType": {
                "_id": "6299fd7504978a2ba513e0a2",
                "name": "General",
                "isNight": false
            },
        "attendances": [
            {
                "_id": "642328a42746f65c2480a140",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-03T04:05:00.000Z"
            },
            {
                "_id": "642328a42746f65c2480a141",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-03T13:45:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-04",
        "shift": {
            "_id": "6422d10a994726677e9105c5",
            "date": "2023-04-04",
            "shiftType": {
                "_id": "6299fd7504978a2ba513e0a2",
                "name": "Next Day End",
                "isNight": true
            }
        },
        "attendances": [
            {
                "_id": "6423292f2746f65c2480a142",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-04T12:30:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-05",
        "shift": null,
        "attendances": [
            {
                "_id": "6423292f2746f65c2480a143",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-04T21:55:00.000Z"
            }
        ]
    }
]

I have shift allocation table where I am recording the night shift as “isNight”: true”. I need the output looks like bellow.

[
    {
        "date": "2023-04-01",
        "shift": {
            "_id": "6422d103994726677e9105c3",
            "date": "2023-04-01",
            "shiftType": {
                "_id": "6299fd7504978a2ba513e0a2",
                "name": "Next Day End",
                "isNight": true
            }
        },
        "attendances": [
            {
                "_id": "6422d1b9994726677e9105c6",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-01T05:30:00.000Z"
            },
            {
                "_id": "6422d1b9994726677e9105c7",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-02T00:30:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-02",
        "shift": null,
        "attendances": [
            {
                "_id": "6423286e2746f65c2480a13e",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-02T04:30:00.000Z"
            },
            {
                "_id": "6423286e2746f65c2480a13f",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-02T12:40:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-03",
        "shiftType": {
                "_id": "6299fd7504978a2ba513e0a2",
                "name": "General",
                "isNight": false
            },
        "attendances": [
            {
                "_id": "642328a42746f65c2480a140",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-03T04:05:00.000Z"
            },
            {
                "_id": "642328a42746f65c2480a141",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-03T13:45:00.000Z"
            }
        ]
    },
    {
        "date": "2023-04-04",
        "shift": {
            "_id": "6422d10a994726677e9105c5",
            "date": "2023-04-04",
            "shiftType": {
                "_id": "6299fd7504978a2ba513e0a2",
                "name": "Next Day End",
                "isNight": true
            }
        },
        "attendances": [
            {
                "_id": "6423292f2746f65c2480a142",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-04T12:30:00.000Z"
            },
            {
                "_id": "6423292f2746f65c2480a143",
                "employee": "622061b73b2eaac4b15d42e4",
                "dateTime": "2023-04-04T21:55:00.000Z"
            }
        ]
    }
]

Please help me if possible.

Hi @Pallab_Kole and welcome to MongoDB community forums!!

From the sample documents shared, it looks like you have combination of clean (where shift != null) and dirty data (where shift = null).
If you wish to move documents based on the condition, one idea here could be to create two different collections, one for each of the conditions, and operate between them to achieve the desired results.

Let us know if you have further questions.

Regards
Aasawari