My json to create a record in mongodb is as follows:
{
'timestamp': {
'elasticsearch': {
'backup_status': 'string',
'backup_folder': 'string',
},
'mongodb': {
'backup_status': 'string',
'backup_folder': 'string',
},
'postgresdb': {
'backup_status': 'string',
'backup_folder': 'string',
},
'overall_backup_status': 'string',
},
};
Here the key "timestamp" holds a different value each time, I would like to make this field unique. I have tried check various documentations where I see examples on how to make a field unique by its name but here the field I need to make unique does not have name.
There is no built in way to preserve uniqueness in key names, What I suggest is you maintain an additional fields that will contain those key names, then you can build a unique index on it.
for example your object should look like this:
{
unique_keys = [ 'elasticsearch.backup_status', 'elasticsearch.backup_folder'],
'timestamp': {
'elasticsearch': {
'backup_status': 'string',
'backup_folder': 'string',
},
},
};
On every update you need to maintain the unique_keys field accordingly. Now you can build a unique index on unique_keys, this will protect you from having duplicates as required.