2016-09-17 45 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 
          });