I have a collection in which some documents are in a group.
For example 2 and 3 are in a group:
{_id: 1, subject: "one", groudId: null}
{_id: 2, subject: "two", groudId: "1234"}
{_id: 3, subject: "three", groudId: "1234"}
{_id: 4, subject: "four", groudId: null}
Currently the groupId
gets created at the same time when the documents of the group are created.
The Problem I have is that the design of our project currently creates the group_id
on the client side and the clients then send it to the MongoDB.
I think that this can lead to the problem, that two clients generate the same groupId
for two different groups.
Especially since the grouId
is at this moment a concatenation of the current time and the amount of time the client was running. (Don't ask me why, it wasn't my idea.)
So how can I create a unique id for the groups with MongoDB?
My first Idea was to just take the _id`` of the first task and make that the
groupId`.
For example:
{_id: 1, subject: "one", groudId: null}
{_id: 2, subject: "two", groudId: 2 }
{_id: 3, subject: "three", groudId: 2 }
{_id: 4, subject: "four", groudId: null}
But _id
gets created on insert so I can't set groudId
on insert and have to do a later update.
So is this possible in any way without multiple inserts and/or updates? Or is there a scheme to create a unique group Id on the client?