2016-09-17 48 views
0

我有一个基于MongoDB的羽毛服务:分类和型号如下我应该用猫鼬查询在Feathersjs钩

{ 
    _id : 'SOMEKEY', 
    categoryName : 'XYZ' 
    categoryItems : [ 
    { item_id : 'ITEMID' } 
    ] 
} 

现在我后对我的项目服务挂钩的是在每个创建我想将该Item的引用推入Categories文档中的categoryItems数组中。我怎样才能做到这一点?类别服务的内置方法都不会有帮助。 我应该在我的钩子中写入Mongoose查询吗?如果我这样做,我依赖于Mongoose MongoDB,如果我的数据库更改,我将不得不改变我所有的钩子。

回答

0
const categoryService = hook.app.service('categories'); 
     categoryService.update(hook.data.item_category, 
          { 
           $push: { 
           "category_items": { 
             item_id: hook.result._id 
            } 
           } 
          }, 
          { 
           safe: true, // This value is safely stored in the DB 
           upsert: true // It creates a new object if 
               // a previous one does not exist 
          });