S
S
Skrolea2016-03-14 10:37:12
MongoDB
Skrolea, 2016-03-14 10:37:12

How to make a request in Mongo?

Good afternoon. How to pull data from a specific 'locale'?
I have the following structure

var ArticleSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  title: {
      en : {
          type: String,
          default: '',
          trim: true,
          required: 'Title cannot be blank'
            },
      ru : {
          type: String,
          default: '',
          trim: true,
          required: 'Title cannot be blank'
            },
      fr : {
          type: String,
          default: '',
          trim: true,
          required: 'Title cannot be blank'
          },
      es : {
          type: String,
          default: '',
          trim: true,
          required: 'Title cannot be blank'
          },
      it : {
          type: String,
          default: '',
          trim: true,
          required: 'Title cannot be blank'
          }

  },
...

Get the existing locale with
var locale = i18n.getLocale();//get locale  en
  console.log('locale',locale); //выдаёт en

And how to make a request?
var query = {
    locale:{ $exists : true, $ne : null }
  };
  Article.find(query).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
      if (err) {
        return res.status(400).send({
          message: errorHandler.getErrorMessage(err)
        });
      } else {
        console.log('articles',articles);
        res.json(articles);
      }
    });

What's to stick in query?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-03-16
@AMar4enko

If I understand you correctly, you want something like AS in SQL.
Pseudocode "SELECT title.en as title FROM articles";
But in Mongo you can't do that. As an option - to limit the populate trace. .populate
('title.' + locale), after which, in the model hooks, process these fields like
For information on hooks, look in the documentation for Mongoose (is that it?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question