I have a database with data like this:
[{
"_id": {
"$oid": "664baf560b9539e48fbc6ed1"
},
"name": "first assignment",
"next": {
"$oid": "664baf620b9539e48fbc6ed2"
},
"preview": null,
"date": {
"$date": "2024-04-20T10:00:00.000Z"
}
},
{
"_id": {
"$oid": "664baf620b9539e48fbc6ed2"
},
"name": "second assignment",
"next": {
"$oid": "664baf6d0b9539e48fbc6ed3"
},
"preview": {
"$oid": "664baf560b9539e48fbc6ed1"
},
"date": {
"$date": "2024-04-21T10:00:00.000Z"
}
},
{
"_id": {
"$oid": "664baf6d0b9539e48fbc6ed3"
},
"name": "third assignment",
"next": {
"$oid": "664baf8d0b9539e48fbc6ed4"
},
"preview": {
"$oid": "664baf620b9539e48fbc6ed2"
},
"date": {
"$date": "2024-04-22T10:00:00.000Z"
}
},
{
"_id": {
"$oid": "664baf8d0b9539e48fbc6ed4"
},
"name": "fourth assignment",
"next": null,
"preview": {
"$oid": "664baf6d0b9539e48fbc6ed3"
},
"date": {
"$date": "2024-04-23T10:00:00.000Z"
}
}
]
The next and preview fields refer to other documents in the same collection.
So, suppose I receive the ID of fourth assignment (664baf8d0b9539e48fbc6ed4). I need to create a new document with the same structure in which preview is the ID of the fourth assignment but also update the fourth assignment document to assign the ID of the new created document to next.
Is this flow possible in an aggregation or should I use traditional operations separately?