I have a localization collection where each document is a key-value pair translated into multiple languages.
This is a JSON representation:
[
{ "_id": "form.password" , "en": "Password" , "he": "סיסמא" , "ru": "пароль" }
{ "_id": "form.email" , "en": "Email" , "he": "אימייל" },
{ "_id": "form.firstname", "en": "First Name", "he": "שם פרטי", "ru": "Имя" }
]
This is a mongoose schema:
new Schema({
_id: {
type: String,
required: true
},
en: String,
he: String,
ru: String
});
I need to get all key-values per each language with express.js:
app.get('/api/locals', function(req, res) {
Local.find().select( req.query.language ).exec(function(err, data) {
res.json(data);
});
});
This works fine if each language has the values for all keys but I want to make a fallback where if some language has no value for a certain key then the english value will be used.
I thought maybe I should use aggregation or mapReduce but I don't know how to implement this with mongoose.
With SQL I used to write this query:
SELECT key, COALESCE($language , en) as value FROM locals
Thanks in advance.
I found a solution with the Aggregation framework:
Using $project with $ifNull:
app.get('/api/locals', function(req, res) {
Local.aggregate({
$project: {
value: {
$ifNull: ["$" + req.query.language , "$en"]
}
}
}).exec(function(err, data) {
res.json(data);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With