2017-05-17 14 views
0

文字列がshop_nameまたはnameのスキーマ内のfoodに存在するかどうかを調べたいのですが、ここで Mongooseが正常に動作しないか、または正常に動作しない

は、ここで私は上記が私を与える文字列のいずれかで shop_nameまたは food.name

に一致するレコードを表示したい私のスキーマ

var storeSchema = { 
    "area": String, 
    "food": [{ 
     'name': String, 
     'description': String 
    }], 
    "shop_name": String 
}; 

のクエリ

dbModel.stores.find({ "food.name": { '$regex': re } }).or([{ 'shop_name': { '$regex': re } }]).exec(function(err, docs) { 
     if (!err) { 
      res.status(200).json({ 
       "success": "1", 
       "message": docs 
      }); 
     } else { 
      res.status(200).json({ 
       "success": "0", 
       "message": "No result found" 
      }); 
     } 
    }); 

です文字列が両方の要素に含まれている場合にのみレコードを返します。 orで確認するにはどうすればいいですか?shop_nameやfood.nameが一致した場合は、それが表示されます。

答えて

0

$orの配列内に条件を配置する必要があると思います。

foodはオブジェクトの配列なので、最初の条件で$elemMatchを使用する必要があります。

私は前にこれをやったことがないが、それは次のようになります。

dbModel.stores.find({ 
    $or: [ 
     { 
      food: { 
       $elemMatch: { name: { $regex: re }} 
      } 
     }, 
     { 
      shop_name: { $regex: re } 
     } 
    ] 
}).exec(...) 

Another reference

関連する問題