使用填充方法在Sails Mongo中建立深层关联? [英] Deep associations in sails mongo using populate method?

查看:68
本文介绍了使用填充方法在Sails Mongo中建立深层关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是sails.js的新手,并且正在使用"sails.js与Mongodb" . 我在帆应用程序中使用填充存在深度关联问题.

I am new to sails.js and I am using "sails.js with Mongodb". I am having problem with deep associations using populate in my sails app.

我有这样的关系:

 Category has many to many relationship with Article.

 City has one to many relationship with Areas.

 Article has one to one relationship with City and Areas.

Category.js

module.exports = {

schema: true,

attributes: {

    //add referecnce to other article
    Articles: {
            collection: 'Article', 
            via:'ref_category_id' 
      },

    category_name: { 
        type:'string',  
        required: true, 
        unique: true },
  }
};

Article.js

module.exports = {

schema: true,

attributes: {

       //adding reference to category
        ref_category_id: {
                    collection:'Category',
                    via:'Articles'
         },

    //adding reference to city
     ref_city_id: {
                    model:'City'
       },

    //add a reference to area
         ref_area_id: {
                  model:'Areas'
        },

      //adding reference to tags
      tags: {
           collection: 'Tag', 
           via:'articles'
       },

      title:{ type:'string',required:true},

      blurb: { type: 'string'},

      description:{ type:'string'}
    }
 }; 

City.js

module.exports = {

        schema: true,

        attributes: {


        Areas: {
                collection: 'Areas', 
                via:'ref_city_id'
        },

         ref_article_id:{
                    model:'Article'
         },


        city_name: { type:'string',
                   required:true,
                   unique:true }
    }
 };

Areas.js

 module.exports = {

   schema: true, 

   attributes: {

        area_name: { type:'string',required:true},

        latitude: { type:'float'},

        longitude: { type:'float'},

        ref_city_id: {
                model: 'City' 
        },

        ref_article_id:{
             model:'Article'
         }

     }
 };

Tag.js

   module.exports = {

      schema:false,

      attributes: {

         //adding reference to article 
          articles: {
                  collection: 'Article', 
                  via:'tags'
          },

      tag_name:{type:'string',
                required:true,
                unique:true
         }
     }
  };

CategoryController.js

  searchCategory: function(req, res, next) {

    var category_name = req.param('category_name');

    Category.find({category_name:{'like': '%'+category_name+'%'}}).populate('Articles').exec(function(err, category) 
    {

        if(err)
        {
         return res.json({'status':486,'status_message':'Server Error'});
        }
        else
        { 
            if(category.length > 0)
            {
                 var c = parseInt(category[0].Articles.length,10);

                console.log(c);
                var i = parseInt('0',10);

                for (i=0; i<c; i++)
                {
                    console.log('i value in loop = ' + i);
                    Article.find({id:category[0].Article[i].id}).populateAll().exec(function(err,article_info) {
                        if(err)
                        {
                            return res.send(err);
                        }
                        else
                        {
                            console.log(article_info);
                            console.log('-------------------------------------------');

                            res.json(article_info); 
                            console.log(' I value = ' + i); 
                        }
                    }); 

                } 
                //console.log(category);
                //return res.json({'status':479,'status_message':'Success..!!','category_info':category});
            }
            else
            {
                return res.json({'status':489,'status_message':'failure..!! No categories found..!!'});
            }
        }
    });
}

邮递员请求:

 http://localhost:1337/category/searchCategory

 {"category_name":"travel"}

这是我的json响应: 在这里,我只获得与类别对应的文章.但我想显示映射到文章的城市,区域和标签的值.

This is my json response: Here I am getting only article which is mapped with category. but i wanted to display the values of city, areas and tags which are mapped to article.

  {
   "status": 479,
   "status_message": "Success..!!",
   "category_info": [
     {
        "Articles": [
            {
                "ref_city_id": "55a766d0a29811e875cb96a1",
                "ref_area_id": "55a78b69578393e0049dec43",
                "title": "title",
                "blurb": "blurb",
                "description": "Description",
                "createdAt": "2015-07-16T12:36:36.778Z",
                "updatedAt": "2015-07-16T12:48:20.609Z",
                "id": "55a7a55439ace79e0512269d"
            },
        ],
        "category_name": "Cooking ",
        "id": "55a6b26aee9b41de747547bb",
        "createdAt": "2015-07-15T19:20:10.670Z",
        "updatedAt": "2015-07-15T19:20:10.670Z"
    }
  ]
}

如何使用填充进行深层嵌套关联?还是有其他方法可以实现这一目标?

How to do the deep nested associations using populate? or Is any other way to achieve this?

请有人可以帮助我实现这一目标.

Please Can anyone help me to achieve this.

先谢谢了.

推荐答案

是的,还有另一种方法.

Yes, there is another way to do this.

确保您需要以下npm模块.

Make sure you require the following npm module.

var nestedPop = require('nested-pop');

 

searchCategory: function(req, res, next) {
  var category_name = req.param('category_name');
  Category.find({category_name:{'like': '%'+category_name+'%'}})
  .populate('Articles')
  .exec(function(err, category) {
    if(err) return res.json({'status':486,'status_message':'Server Error'});
    if(category.length > 0) {
      var c = parseInt(category[0].Articles.length,10);
      console.log(c);
      var i = parseInt('0',10);
      for (var i = 0; i < c; i++) {
        console.log('i value in loop = ' + i);
        Article.find({id:category[0].Article[i].id})
        .populateAll()
        .exec(function(err, article_info) {
          if(err) return res.send(err);
          return nestedPop(article_info, {
            ref_city_id: {
              as: 'City',
              populate: [
                'things',
                'you',
                'want',
                'to',
                'populate',
                'for',
                'city'
              ],
            },
            ref_area_id: {
              as: 'Area', // or Areas (whatever the model name is)
              populate: [
                'things',
                'you',
                'want',
                'to',
                'populate',
                'for',
                'area'
              ]
            }
          }).then(function(article_info) {
            console.log(article_info);
            console.log('------------------------------------------');
            res.json(article_info); 
            console.log(' I value = ' + i);
          });
        }); 
      }
    } else {
      return res.json({'status':489,'status_message':'failure..!! No categories found..!!'});
    }
  });
}

转到 https://www.npmjs.com/package/nested-pop有关更多文档.

这篇关于使用填充方法在Sails Mongo中建立深层关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆