2017-08-23 6 views
0

"testProjection"コレクションに以下のデータが挿入されています。Mongodb Projectionのトップレベルネストされたフィールドよりも、低レベルの入れ子フィールドが優先

{ 
    "eventname" : "Ball Passed", 
    "command" : { 
     "name" : "Run", 
     "strike" : 15, 
     "score" : true, 
     "duration" : 123 
    } 
}, 
{ 
    "eventname" : "Ball Passed", 
    "command" : { 
     "name" : "Run", 
     "strike" : 12, 
     "score" : false, 
     "duration" : 597 
    } 
} 

私のクエリは次のとおりです。

db.testProjection.find({},{"command":0,"command.score":0}) 

Actaul結果:

{ 
    "_id" : ObjectId("599d27944be6e513549dd170"), 
    "eventname" : "Ball Passed", 
    "command" : { 
     "name" : "Run", 
     "strike" : 15.0, 
     "duration" : 123.0 
    } 
}, 
{ 
    "_id" : ObjectId("599d27944be6e513549dd171"), 
    "eventname" : "Ball Passed", 
    "command" : { 
     "name" : "Run", 
     "strike" : 12.0, 
     "duration" : 597.0 
    } 
} 

期待される結果:

{ 
    "_id" : ObjectId("599d27944be6e513549dd170"), 
    "eventname" : "Ball Passed" 
}, 
{ 
    "_id" : ObjectId("599d27944be6e513549dd171"), 
    "eventname" : "Ball Passed" 
} 

MongoDBの投影では、MongoDBの中クエリエンジンがありますネストされた低レベル投影を好む最上位レベルの投影。

mongodbでこの動作が望ましいですか? mongodbクエリを使用して私の期待どおりに達成する方法はありますか?

+0

「db.testProjection.fi」の結果を確認することもできますnd({}、{"command.score":0、 "command":0}) ';)。 –

+0

はい、動作が期待され、期待されています。さらに、 'db.testProjection.find({}、{" command.score ":0、" command ":0})'の回りに別の方法を指定した場合、サブプロパティは削除され、あなたは 'command:{}'を残しています。したがって、db.testProjection.find({}、{"command.score":0、 "command.duration":0}) 'のように、パスを"スタック "するのが一般的です。 "すべて"、次にあなたはそれだけを発行します。しかし、それは実際には "優先順位"ではなく、 "優先順位"です。 –

答えて

1

MongoDbクエリを使用して私の予想外の結果を達成する方法はありますか?

あなたはご期待フィールドの代わりに、含めることができるのような INGのフィールドを除外する:

db.testProjection.find(
    {}, 
    { 
     _id: 1, 
     eventname: 1 
    } 
); 

をしかし、私は、それはこのように集計を使用することをお勧めだと思う - パフォーマンスの問題として - :

db.testProjection.aggregate(
    [ 
     { 
      $project: { 
       _id : 1, 
       eventname: 1 
      } 
     }, 
    ] 
); 
関連する問題