2017-02-18 4 views
1

私はフィールドとして場所と名前を持つコレクションを持っています。なぜmongoはすべてのフィールドを返さないのですか?

私はrobomongoでこれを実行すると私はこれを実行すると、それは、すべての12個のフィールドを返す

db.getCollection('_event').find({"location.country":"United States"}) 

しかし、以下のようなマングースと名前を

eventSchema.index({name: 'text'}); 

をインデックスを作成しましたrobomongo、idとlocationの2つのフィールドのみを返します。

db.getCollection('_event').find({$text: {$search: "2017 Honda"}},{"location.country":"United States"}) 

答えて

3

db.getCollection('_event').find(
    {$text: {$search: "2017 Honda"}}, // <-- query part 
    {"location.country":"United States"} // <-- projection part 
) 

あなたは、クエリオブジェクトに移動する必要がありますよう:

db.getCollection("_event").find(
    { 
     "$text": { "$search": "2017 Honda" }, 
     "location.country": "United States" 
    } 
) 

あなたは、あなたが2つのフィールドを持つ投影を取得しているので、投影として、それを指定し、あなたの他のクエリ式を置き忘れてきましたこれは暗黙$and表現であり、また、明示的

db.getCollection("_event").find(
    { 
     "$and": [ 
      { "$text": { "$search": "2017 Honda" } }, 
      { "location.country": "United States" } 
     ] 
    }   
) 
-1
db.getCollection('_event').find({ 
    $text: {$search: "2017 Honda"}, 
    "location.country":"United States" 
}, 
{ 
    "location":1, 
    "_id":0 // -- here we specify required fields. 1 is showing, 0 is not showing 
}); 
として指定することができ
関連する問題