2016-12-28 43 views
0

我得到了以下sequelize查詢片段,其中我喜歡做的多notLike查詢:多notLike不需額外工作

article 
    .findAndCountAll({ 
     where: { 
     NAME: { 
      $like: '%'+req.body.search+'%' 
     }, 
     ITEM_NUMBER: { 
       $notLike: 'MF%', 
       $notLike: 'OLS%', 
       $notLike: 'MV%', 
       $notLike: 'MD%', 
       $notLike: 'AE%' 
     }, 
     PRICE: { 
      $gt: 0 
     }, 
     BLOCKED_FROM: { 
      $or: { 
      $gt: new Date(), 
      $eq: null 
      } 
     } 
     }, 
     limit: 20 
    }) 
    .then(function(result) { 
     res.json(result.rows); 
     //res.json(result.count); 
     console.log(result.count); 
     console.log(result.rows); 
    }); 

的問題是,對於地方部分ITEM_NUMBER僅接縫搭末notLike線($ notLike:'AE%')。由此產生的查詢輸出顯示:

SELECT TOP 20 [NAME], [ITEM_NUMBER], [PRICE], [BLOCKED_FROM] FROM [ARTIKEL] AS [ARTIKEL] WHERE [ARTIKEL].[NAME] LIKE N'%testname%' AND [ARTIKEL].[ITEM_NUMBER] NOT LIKE N'AE%' AND [ARTIKEL].[PRICE] > 0 AND ([ARTIKEL].[BLOCKED_FROM] > N'2016-12-28 17:05:58.750' OR [ARTIKEL].[BLOCKED_FROM] IS NULL);

查詢應該包含所有%notLike參數。任何想法有什麼不對? THX 英戈

回答

0

試試:

... 
    ITEM_NUMBER: { 
     $or: [{ 
      $notLike: 'MF%' 
     }, { 
      $notLike: 'OLS%' 
     }, { 
      $notLike: 'MV%' 
     }, { 
      $notLike: 'MD%' 
     }, { 
      $notLike: 'AE%' 
     }] 
    }, 
    ... 
+0

這很有趣。有用。在操作員或操作員之後,我已經嘗試過沒有啓動括號。我只拿了大括號,如[組合章節](http://docs.sequelizejs.com/en/v3/docs/querying/)中的文檔所示,其中第一個或運算符示例沒有方括號。 .Thx爲此! – Inge

+0

很高興幫助@Inge –

0

當使用陣列相同的密鑰只需要之一,那麼,請嘗試更換這樣的:

article 
.findAndCountAll({ 
    where: { 
    NAME: { 
     $like: '%'+req.body.search+'%' 
    }, 
    ITEM_NUMBER: { 
      $notLike: { $any: ['MF%','OLS%','MV%','MD%','AE%']} 
    }, 
    PRICE: { 
     $gt: 0 
    }, 
    BLOCKED_FROM: { 
     $or: { 
     $gt: new Date(), 
     $eq: null 
     } 
    } 
    }, 
    limit: 20 
}) 
.then(function(result) { 
    res.json(result.rows); 
    //res.json(result.count); 
    console.log(result.count); 
    console.log(result.rows); 
}); 

Operator of wherehttp://docs.sequelizejs.com/en/latest/docs/querying/?highlight= $ notLike

+0

我已經嘗試過,但它然後會引發錯誤'未處理的拒絕SequelizeBaseError:關鍵字'ANY''附近的語法錯誤。任何想法? – Inge

+0

而我沒有錯ANY只適用於PG?我在mssql-server上。 – Inge