Can you create a unique hash for each element in an array?

Hi, so I have an entry in my collection as such:

{
    _id: someID
    array: [
        {
            title: someTitle
            data: someOtherData
            ...
        },
        {
            title: someTitle
            data: someOtherData
            ...
        }
    ]
}

Is there any way for me to create a unique and non-changing _id field for each element in this array? I understand that the array indexing is already unique, and I don’t have a constraint of duplicates as the data will never result in duplicates (consists of unique hashes).

The reason why I seek the indexing to be unique and non-changing is because I’m using the index to generate a link to some data. Right now it directs to example.com/some_path/userID/0 and the last digit is the index in the array. However, users can modify this data and delete data at an arbitrary index.

That means that if one day you navigate to that link, and the user deletes their item at index 0, the next day when you navigate to that link you’ll be seeing something different (the element at the previous index 1 instead). Or, if you navigate to the last element in the array and the user deletes something before that, it’ll result in a 404.

If I can have a unique ID, then links are preserved over time. I thought of one way of doing this by using an object instead of an array:

{
    _id: someID
    object: {
        {
            _id: someUniqueID,
            title: someTitle
            data: someOtherData
            ...
        },
        {
            _id: someUniqueID,
            title: someTitle
            data: someOtherData
            ...
        }
    }
}

But I’d prefer to use an array as it seems much more logical than using an object of objects, as I still have the choice of accessing things by index and all the other useful sorting, etc that can be done with an array.

Appreciate any help, thank you!

Hi @Ajay_Pillay

Welcome back to MongoDB community.

Why not to set a new objectId for each array element, you can potentially index it and query it .

Creating objectIds is the preferred bson way of generating ids in MongoDB, will that work or you have to have an ability to recreate it from some other document data?

The array makes sense as object can have multiple sub objects only under a fields (what you presented is not a legal json representation)

Hopefully this helps.

Thanks
Pavel

1 Like

Hi, thank you for your response.

So I realized the best way for me to do this is to use data I already have generated.

In my sub-fields, I actually have unique hashed IDs that are being generated, and I figured I could just use the very first unique hashed ID as the ID for the entire object.

And yes I realize that I made a mistake in the JSON representation, the _id field should’ve been hoisted up one level.

Thanks for pointing out the ObjectId() function, it should be useful in some other areas I’m developing.

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