I am storing numbers as values for a certain field in my mongodb (with mongoose). The reason to store numbers is so that I can do some math on them.
When I get the values back trough an ajax call I would like to display a string instead of the number.
So let's say I get this JSON object back from the db:
[ {id : '1', name: 'john smith', level: '3'},
{id : '2', name: 'peter miller', level: '2'},
{id : '3', name: 'susan black', level: '1')]
When I display this in a table I want to replace '1' with 'beginner', '2' with 'experienced' and '3' with 'expert'. Ideally I would have a 2nd Object that can store these translations such as [ '1' : 'beginner', '2': experienced, '3': 'expert']
I assume I have to iterate over each key/value pair and replace the old value with a new value. Should this be done on the client side (jQuery?) or server side. I am using node.js, express mongodb(mongoose).
Thanks!
For such situations you can implement several solution with both advantages and disadvantages:
If you have a small dictionary that needs to be mapped it is a safe bet to move it to the client side and let them do the processing for you.
However if you have a large dictionary that needs to be mapped, maybe storing the data in MongoDB might not be that bad.
Based on the use-case extracted from your example I would suggest the first case - move the processing on the client side. Thus your application will be as fast as the client side computations will be and you will end up saving money on your end.
Thanks Simon and Roberto,
I decided to go for the client side solution:
var replaceValues = $.each(oldValue, function(){
if (this.level == 1) {
this.level = 'beginner';
}
if (this.level == 2){
this.level = 'experienced';
}
if (this.level == 3){
this.level = "expert";
}
});