如何使用FeatherJS处理多对少mongodb关系?

如何使用FeatherJS处理多对少mongodb关系?,mongodb,mongoose,feathersjs,feathers-service,Mongodb,Mongoose,Feathersjs,Feathers Service,我有两个模型,具有多对少关系,我正在建模如下: // Portfolio const portfoliosSchema = new Schema({ name: { type: String, required: true }, description: { type: String }, user: { type: Schema.Types.ObjectId, ref: 'User', required: true }, positions: [{ stock: { t

我有两个模型,具有多对少关系,我正在建模如下:

// Portfolio
const portfoliosSchema = new Schema({
  name: { type: String, required: true },
  description: { type: String },
  user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
  positions: [{
    stock: { type: Schema.Types.ObjectId, ref: 'Stock', required: true },
    cost: number,
    amount: number,
  }]
});

//  Stock
const stocksSchema = new Schema({
  exchange: { type: String, required: true },
  symbol: { type: String, required: true },
  company: { type: String },
  description: { type: String }
});
如果不以友好的方式编写自定义服务,我将如何:

  • 查询投资组合并从股票中填充相关记录 收藏
  • 简化组合模式中嵌套位置的插入/更新(即不更新整个记录)
  • 这是否受支持/我是否应该编写自定义服务和/或规范关系

    编辑-解决了#1使用前钩子获取额外数据的第一个问题:

    function(hook) {
      const query = hook.params.query;
      hook.params.query = Object.assign({},query,{
        $populate: {
          path: 'positions.stock',
          select: 'exchange symbol'
        }
      });
    }
    

    填充代码示例,根据您的需要进行调整:

    Portfolio.find({ some: 'params' })
    .populate({ path: 'stock', select: 'name -_id' })
    .exec((err, portfolios) => {
      res.json({ portfolio: portfolios });
    });
    
    正在更新嵌套数组项:

    Position.update({ 'position._id': 'h43q9h94945d948jh098j6h0' }, { 
      '$set': { 'position.$.whatever': 'your-updated-stuff' }
    }, err => {
      if (err) return console.log(err));
      res.json({ success: true });
    });
    

    是的,这两个都受支持,正在为您寻找一些示例代码。嗨,艾伦,如果我错了请纠正我,但这是如何创建这些查询的mongoose代码。我将如何在feathersjs中使用它-我必须使用这些查询创建自定义服务吗?