How do I construct a key (like mobile no) in a key-value DB when I want to store several instances of the key as well?

StackOverflow https://stackoverflow.com/questions/21928525

Pergunta

I want to store JSON documents eg {"mobile_no":"1234567","deactived_date":"2010/5/5","name":"customer1"} with the mobile number as the key in a NoSQL database like CouchBase.

As mobile numbers are being retired over time and re-used with other customer, how can I store several instances of the same mobile number whilst reflecting within the key that 1-to-many are deactivated ones and 1 is always the actual one active on the network?

In a relational DB that wouldn't be a problem, I would store the data like this:

mobile_no deactivated_date
1234567   2010/05/05
1234567   2011/12/14
1234567   NULL

In a key-value database the document key needs to be unique, so my question is how would one built a unique key for the three examples above whilst still enabling to query it using the mobile number (eg you would check if there is an entry in the DB with that mobile number and upon finding an active number give out an error message).

Is there a possibility to search for only a part of a key?

Foi útil?

Solução

Or you might implement something like versioning for the keys. In this case the current version will be stored under the "1234567" key. Lets imagine that the number was reused twice. In this case your bucket will have the following keys:

"1234567"
{"version": 2, "mobile_no": "1234567", "deactivated_date": null}

"1234567:1"
{"version": 1, "mobile_no": "1234567", "deactivated_date": "2011/12/14"}

"1234567:0"
{"version": 0, "mobile_no": "1234567", "deactivated_date": "2010/05/05"}

So each time you are deactivating your document, you read the current version, add this version to the key and copy all the content there. After that you increment the version and store a new document to the default key (without suffix)

Outras dicas

A common pattern in NoSQL databases like Couchbase would be to have a single document with both the current and deactivated dates in it - something like:

  • Key: mobile:1234567

  • Value:

{ "mobile_no"         : "1234567",
  "deactivated_dates" : [ "2010/05/05", "2011/12/14" ]
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top