What to use for subdocuments ID's in MongoDB?

132 views Asked by At

I have collection with documents in MongoDB database. Each document (see below) has _id and might contain number of subdocuments in grades array. I was wondering what is the best practice to create unique ID's for these subdocuments? I could use ObjectId or generate some random ID's on application level, so what would be the best approach? BTW I use NodeJS with Mongo native driver.

{
    "_id" : ObjectId("556c42c0563c07735bcd87dd"),
    "name" : "Homer Simpson",
    "grades" : [
// use ObjectId as below?
        {
            "id" : ObjectId("556c42c0563c07735bcd87db"),
            "level" : 0
        },
// or randomly generated string?
        {
            "id" : "556c42c0563c07735bcd87dc",
            "level" : 1
        }
    ]
}
0

There are 0 answers