2016-10-12 13 views
3
{ 
    "_id":objectId(23651478), 
    "name":"Tomatos" 
    "array":[ 
      {"title":"Vegetables"} 
      ] 
    "description":"Vegitables are good to health" 
}, 
{ 
    "_id":objectId(45761244), 
    "name":"Apples" 
    "array":[ 
      {"title":"Fruits"} 
      ] 
    "description":"Fruits are good to health, vegitables are also good to health" 
}, 
{ 
    "_id":objectId(45761244), 
    "name":"Apples" 
    "array":[ 
      {"title":"Vegetables-home-made"} 
      ] 
    "description":"Fruits are good to health, vegitables are also good to health" 
} 

上記は私のプロジェクト要件のための私のモンゴスキーマモーダルです。私が野菜のみを検索したいときは、野菜の名前を付ける必要があります。野菜と果物の両方を検索すると、トマトとアップルの両方の名前が来るはずです。mongodbの配列でテキスト検索を見つけるには

私はこの

{"array":{$in:[{"title":"Vegetables"},{"title":"Fruits"}]}} 

ようですが、タイトルVegetablesなくVegetables-Home-madeを持つ文書のみを与えている"$in"演算子を使用していました。そのために私は$elemMatch

{"array":{$elemMatch:[{"title":"Vegetables"},{"title":"Fruits"}]}} 

を使用しようとしました。しかし、我々はすなわち、elemMatchでVegetablesFruitsは、それがエラーを与えている倍数を検索したとき、私はつまり、特定のタイトルキーの配列内のテキストを検索します。 、MongoDBの中

{"array":{$text:{:$search:[{"title":"Vegetables"},{"title":"Fruits"}]}}} 
+0

正規表現$ regexまたは$ textを使用することができます。これを試してください:{"array":{$ in:{{"title":{$ regex:{/ Vegetables /}}}}} – chirag

答えて

2

は何をしたいです:

は、次のような演算子で$内の正規表現を使用することができます。

{array: { $elemMatch: { title: { $in: [/Vegetables/, /Fruits/] }}}} 
0

一般的なテキスト検索は以下のようになります。

db.collectionName.find({ $text: { $search: "Vegetables" } }) 

あなたがのrunninされている検索テキストを確認してくださいグラム単語野菜は、その中にスペルエラーが含まれています。ここ

{"array":{$text:{:$search:[{"title":"Vegitables"},{"title":"Fruits"}]}}} 
関連する問題