【问题标题】:MEAN stack: How to create routes for nested relationships?MEAN stack:如何为嵌套关系创建路由?
【发布时间】:2016-05-28 08:51:33
【问题描述】:

我有两个模型用户和计划。用户有很多计划,所以在我的 Mongoose Schema 中,我嵌入了如下所示的用户 ID。

var PlanSchema = new mongoose.Schema({
   title: {type: String},
   userId: {type: String},
   spots:[{
     name: {type: String},
     category: {type: String},
     address: {type: String},
     hours: {type: String},
     phone: {type: String},
     website: {type: String},
     notes: {type: String},
     imageUrl: {type: String},
     dayNumber: {type: Number}
   }]
});

我在不同的文件(users.js 和 plan.js)中有两个单独的控制器,用于处理用户和计划(/api/users 和 /api/plans)的 api 路由。

如果我有特定用户的用户 ID,我正在努力弄清楚如何找到特定用户的计划。

我应该在用户控制器中创建路由 /api/users/:id/plans 还是有办法在像 /api/plans/search 这样的 plan.js 控制器中创建搜索路由?=

或者有其他方法吗?

这是我的计划控制器代码:

计划

// Routes
PlansController.get('/', function(req, res){
  Plan.find({}, function(err, plans){
  res.json(plans);
  });
});

PlansController.get('/:id', function(req, res){
  Plan.findOne({_id: req.params.id}, function(err, plan){
    res.json(plan);
  });
});



PlansController.delete('/:id', function(req, res){
  var id = req.params.id;
  Plan.findByIdAndRemove(id, function(){
    res.json({status: 202, message: 'Success'});
  });
});

PlansController.post('/', function(req, res){
  var plan = new Plan(req.body);
  plan.save(function(){
    res.json(plan);
  });
});

PlansController.patch('/:id', function(req, res){
  Plan.findByIdAndUpdate(req.params.id, req.body, {new: true}, function(err, updatedPlan){
    res.json(updatedPlan);
  });
});

不确定什么是最好的方法。非常感谢您的意见。

【问题讨论】:

    标签: javascript node.js express mongoose mean


    【解决方案1】:

    1) 如果你想创建这个路径 '/api/users/:id/plans' 那么你需要像这样将计划数组嵌入到用户模式中 -

    var Schema = mongoose.Schema;
    
    var UserSchema = new mongoose.Schema({
      ...
      plans: { type: [Schema.ObjectId], ref: 'Plan' },
      ...
    });
    mongoose.model('User', UserSchema);
    

    现在您可以通过运行此查询轻松地从计划模型中填充计划 -

    User.findOne({ _id: userId})
      .populate('plans')
      .exec(function(err, user){
        //do stuff
      });
    

    如果用户可以拥有的计划数量不是无限的,那么您可以使用它,因为 mongodb 文档的大小限制为 16 MB,并且超出范围的数组不应包含在架构中。

    2) 要使用“/api/plans/search?userid=”,您需要将 userId 存储在 Plan 架构中。您应该对架构进行一些更改,如下所述。

     var PlanSchema = new mongoose.Schema({
       title: {type: String},
       userId: { type: Schema.ObjectId, ref: 'User' },
       spots:[{
         name: {type: String},
         category: {type: String},
         address: {type: String},
         hours: {type: String},
         phone: {type: String},
         website: {type: String},
         notes: {type: String},
         imageUrl: {type: String},
         dayNumber: {type: Number}
       }]
    });
    

    因此,将来如果您需要用户数据,您可以使用上面提到的相同方法轻松填充它,而无需运行嵌套查询。

    希望对您有所帮助。

    【讨论】:

    • 谢谢,图沙尔。如果计划不是无限期的,您认为哪种方式更好。使用第二种方式有缺点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2015-07-20
    • 1970-01-01
    • 2022-08-17
    • 2018-04-13
    相关资源
    最近更新 更多