Answer the question
In order to leave comments, you need to log in
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'
}
},
...
var locale = i18n.getLocale();//get locale en
console.log('locale',locale); //выдаёт en
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);
}
});
query
?
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question