2017-03-22 8 views
0

私の現在のプロジェクトで、ユーザーが異なるフィルタを選択して特定の画像を検索できる検索機能を実装するギャラリーサービスを作成しました。バックエンドでは、多くのフィルタが画像に割り当てられます。Sequelizeアソシエーションで複数の値を検索する

私の問題は、私は私のギャラリーサービス(例えば、以下のfilterIDs:23、42)に複数のフィルタを与えるときということです、それはフィルター#23ORを含み、それらの画像フィルタ#を返します。両方ともに、012またはです。両方のIDを含む画像のみを取得する予定です。

picture.model.js

sequelize.define("Picture", { 
    //... 
    picturePath: { 
    type: DataTypes.STRING 
    } 
}, { 
    classMethods: { 
    associate: function(models) { 
     Picture.belongsToMany(models.Filter, { as: 'filters', through: 'PictureFilter' }; 
    } 
} 

filter.model.js

sequelize.define("Filter", { 
    //... 
    name: { 
    type: DataTypes.STRING 
    } 
} 

私が試したどのようなクエリ

model.Picture.findAndCountAll({ 
    where: { 
    $ne: null 
    }, 
    include: [ 
    { 
     model: models.Filter, 
     as: 'filters', 
     where: { 
     id: { 
      $in: [23, 42] 
     } 
     } 
    } 
    ] 

経験豊富結果:

[ 
    { 
     ..., 
     "rows": [ 
     { 
      ..., 
      "filters": [ 
      { 
       "id": 23, 
       ... 
      } 
      ] 
     }, 
     { 
      ..., 
      "filters": [ 
      { 
       "id": 23, 
       ... 
      }, 
      { 
       "id": 42, 
       ... 
      } 
      ] 
     } 
     ] 
    } 
    ] 

期待される結果:

[ 
    { 
     ..., 
     "rows": [ 
     { 
      ..., 
      "filters": [ 
      { 
       "id": 23, 
       ... 
      }, 
      { 
       "id": 42, 
       ... 
      } 
      ] 
     } 
     ] 
    } 
    ] 

は、事前にありがとうございます!

答えて

-1

利用$contains代わりの$in以下の例を参照してください。

id: { 
    $contains: [23, 42] 
} 
関連する問題