Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb coalesce equivalent (with mongoose)

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.

like image 392
Ilan Frumer Avatar asked Feb 02 '14 15:02

Ilan Frumer


1 Answers

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);
  });
});
like image 156
Ilan Frumer Avatar answered Sep 23 '22 17:09

Ilan Frumer