简体   繁体   English

如何使用i18n建立对Mongo的查询?

[英]How to build a query to Mongo with i18n?

I'm using i18n-node in my express app and i need to get data from database with getLocale() "prefix" 我在Express应用程序中使用的是i18n节点,我需要使用getLocale() “ prefix”从数据库中获取数据

updated question 更新的问题

My mongoose Schema 我的猫鼬图式

 var mongoose = require('mongoose'),
   Schema = mongoose.Schema; 
   var ArticleSchema = new Schema({
      title: {
        en : {type: String},
        it : {type: String},
        fr : {type: String},                 
          },
    content: {
        en : {type: String},
        it : {type: String},
        fr : {type: String},                 
          },

MyController 我的控制器

  exports.list = function (req, res) {
       var locale = i18n.getLocale();
       console.log('locale',locale); //working -> en

     Article.find({'title.locale':locale}).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');
    res.json(articles);
  }
});
};

I want to get all articles with en (eg) prefix. 我想获取所有带有en (例如)前缀的文章。 getLocale() method returns a locale like en , it etc. How can i build a query "Find all articles where "title (or content) : getLocale()" ? Even Article.find({'title': 'en'}) not working getLocale()方法返回像区域enit等我如何建立一个查询“找到所有的文章,其中‘称号(或内容):的getLocale()’即使Article.find({'title': 'en'})不起作用

I believe it's hard to fetch a subdocument using its key field alone.You need to provide a value of the subdocument eg Article.find({'title.en' : 'sample' }) . 我认为仅凭其关键字段就很难获取子文档。您需要提供子文档的值,例如Article.find({'title.en' : 'sample' }) [Update] [更新]

I hope this will fix your problem 我希望这可以解决您的问题

var locale = getLocale();//get locale  returns it, en
var query = {
  'title.'+locale:{ $exists : true, $ne : null }
}
Article.find(query, function(err, articles) {
  console.log(articles);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM