I've recently started with MongoDB on Node.js and I need help from fellow folks on the below situation.
I've already referred Multilingual data modeling on MongoDB which is also similar Schema structure I am following.
I'm creating the list of countries in multi language format and schema as given below.
var CountrySchema = new Schema({
name: {
type: Object,
required: 'Kindly enter the name of the country'
},
Short_code: {
type: String,
required: 'Please enter the country code'
},
Created_date: {
type: Date,
default: Date.now
},
status: {
type: [
{
type: String,
enum: ['published', 'unpublished', 'deleted']
}
],
default: ['published']
}
})
I would like to fetch data in such a way that, when the translation is missing, it should take the default one i.e. en
The equallent SQL is given below.
SELECT Short_code,
CASE
WHEN name.es IS NOT NULL THEN name.es
WHEN name.es IS NULL and name.en IS NOT NULL name.en
END AS name
FROM countries;
Also, it will be greatful if I could pass the es value dynamically to the query.