2017-10-16 6 views
1

2つのフィールドでソートしようとしていますが、優先順位は最初のものになりますが、代わりに優先順位が2番目(lastName)ケース。場合によっては正しく動作しますが、何が変更になるかは不明です。MongoEngine(フラスコ)は正しく集計されません - 毎回今度は

Robo3tをクライアントとして使用して、同じデータベースに接続すると、同じコマンドが正常に動作します。

フラスコを実行すると、以下のコードで、Mongoengineを使用して:Robo3tで、

pipeline = [{"$sort": {"importance": -1, "lastName": 1}}, {"$match": {"visible": True, "editionId": "2017"}}, 
      {"$project": {'firstName': 1, 'lastName': 1, 'biography': 1, 'position': 1, 'workAt': 1, '_id': 0, 
          "imageUrl": 1}}] 
doc = Speakers.objects.aggregate(*pipeline) 

( '真' 'true' になり除く)と同じコード:

db.getCollection('speakers').aggregate([{"$sort": {"importance": -1, "lastName": 1}}, {"$match": {"visible": true, "editionId": "2017"}}, 
      {"$project": {'firstName': 1, 'lastName': 1, 'biography': 1, 'position': 1, 'workAt': 1, '_id': 0, 
          "imageUrl": 1}}], {"collation": {"locale": "en"}} 
    ) 

インデックスをMongoDBの上(3.4 0.9)以下のように見える:

> db.speakers.getIndexes() 
[ 
    { 
     "v" : 1, 
     "key" : { 
      "_id" : 1 
     }, 
     "name" : "_id_", 
     "ns" : "website_v3.speakers" 
    } 
] 

デシベルのバージョンv3.4.9

を使用して0

私は重要度の指標(照合)を作成しようとしましたが、まったく同じことが起こりました。最初は動作しますが、動作しません。

EDIT:

flask_mongoengineは$ソートの順序だけでなく、$プロジェクトをいじっているように思えます。

mongo 
> db.system.profile.find().pretty() 
{ 
    "op" : "query", 
    "ns" : "website_v3.system.profile", 
    "query" : { 
     "find" : "system.profile", 
     "filter" : { 

     } 
    }, 
    "keysExamined" : 0, 
    "docsExamined" : 0, 
    "cursorExhausted" : true, 
    "numYield" : 0, 
    "locks" : { 
     "Global" : { 
      "acquireCount" : { 
       "r" : NumberLong(2) 
      } 
     }, 
     "Database" : { 
      "acquireCount" : { 
       "r" : NumberLong(1) 
      } 
     }, 
     "Collection" : { 
      "acquireCount" : { 
       "r" : NumberLong(1) 
      } 
     } 
    }, 
    "nreturned" : 0, 
    "responseLength" : 98, 
    "protocol" : "op_command", 
    "millis" : 0, 
    "planSummary" : "COLLSCAN", 
    "execStats" : { 
     "stage" : "COLLSCAN", 
     "nReturned" : 0, 
     "executionTimeMillisEstimate" : 0, 
     "works" : 2, 
     "advanced" : 0, 
     "needTime" : 1, 
     "needYield" : 0, 
     "saveState" : 0, 
     "restoreState" : 0, 
     "isEOF" : 1, 
     "invalidates" : 0, 
     "direction" : "forward", 
     "docsExamined" : 0 
    }, 
    "ts" : ISODate("2017-10-19T20:55:30.415Z"), 

} 
{ 
    "op" : "command", 
    "ns" : "website_v3.speakers", 
    "command" : { 
     "aggregate" : "speakers", 
     "pipeline" : [ 
      { 
       "$match" : { 
        "visible" : true, 
        "editionId" : "2017" 
       } 
      }, 
      { 
       "$sort" : { 
        "lastName" : 1, 
        "importance" : -1 
       } 
      }, 
      { 
       "$project" : { 
        "firstName" : 1, 
        "position" : 1, 
        "biography" : 1, 
        "workAt" : 1, 
        "imageUrl" : 1, 
        "_id" : 0, 
        "lastName" : 1 
       } 
      } 
     ], 
     "cursor" : { 

     } 
    }, 
    "cursorid" : 168077246844, 
    "keysExamined" : 0, 
    "docsExamined" : 541, 
    "hasSortStage" : true, 
    "numYield" : 4, 
    "locks" : { 
     "Global" : { 
      "acquireCount" : { 
       "r" : NumberLong(16) 
      } 
     }, 
     "Database" : { 
      "acquireCount" : { 
       "r" : NumberLong(8) 
      } 
     }, 
     "Collection" : { 
      "acquireCount" : { 
       "r" : NumberLong(7) 
      } 
     } 
    }, 
    "nreturned" : 101, 
    "responseLength" : 157635, 
    "protocol" : "op_query", 
    "millis" : 2, 
    "planSummary" : "COLLSCAN", 
    "ts" : ISODate("2017-10-19T20:55:57.691Z"), 
} 
{ 
    "op" : "getmore", 
    "ns" : "website_v3.speakers", 
    "query" : { 
     "getMore" : NumberLong("168077246844"), 
     "collection" : "speakers" 
    }, 
    "originatingCommand" : { 
     "aggregate" : "speakers", 
     "pipeline" : [ 
      { 
       "$match" : { 
        "visible" : true, 
        "editionId" : "2017" 
       } 
      }, 
      { 
       "$sort" : { 
        "lastName" : 1, 
        "importance" : -1 
       } 
      }, 
      { 
       "$project" : { 
        "firstName" : 1, 
        "position" : 1, 
        "biography" : 1, 
        "workAt" : 1, 
        "imageUrl" : 1, 
        "_id" : 0, 
        "lastName" : 1 
       } 
      } 
     ], 
     "cursor" : { 

     } 
    }, 
    "cursorid" : 168077246844, 
    "keysExamined" : 0, 
    "docsExamined" : 0, 
    "hasSortStage" : true, 
    "cursorExhausted" : true, 
    "numYield" : 0, 
    "locks" : { 
     "Global" : { 
      "acquireCount" : { 
       "r" : NumberLong(4) 
      } 
     }, 
     "Database" : { 
      "acquireCount" : { 
       "r" : NumberLong(2) 
      } 
     }, 
     "Collection" : { 
      "acquireCount" : { 
       "r" : NumberLong(2) 
      } 
     } 
    }, 
    "nreturned" : 204, 
    "responseLength" : 303603, 
    "protocol" : "op_query", 
    "millis" : 0, 
    "planSummary" : "COLLSCAN", 
    "ts" : ISODate("2017-10-19T20:55:57.702Z"), 
} 

答えて

0

MongoDBは、(ほとんどの場合)集計ステージを指定した順序で実行します。 $matchオペレータは出力順序を保証しません。

パイプラインの先頭に$match(後に$sort)を入れたいと思っています。これはパフォーマンス上の理由から良い考えです。

+0

私はあなたが示唆したように '$ match'と' $ sort'を入れようとしましたが、同じ結果が得られました。項目は 'lastName'でソートされ、' importance'ではソートされません。 奇妙なことは、どちらの方法でもRobo3Tでは完璧に動作することです。 – gsapu

+0

あなたはあなたの質問をサーバ側で記録し、それらを比較することをお勧めします。フラスコがそれを説明することができるクエリを混乱させる場合。しかし、Robo3Tとあなたのアプリケーションを使用して正確に同じクエリがログに記録された場合、その問題は別の場所にあるはずです:https://stackoverflow.com/questions/15204341/mongodb-logging-all-queries – dnickless

+0

その他の潜在的な混乱要因は照合設定値、混合フィールドタイプ(intや重要度の文字列など)、NULL値、値の先頭の余分なスペース文字、フィールド名のタイプミス。最後に、ソートの別の方法をテストすることもできます。問題を特定するために 'find()'または他の2つの段階を使わないでください。 – dnickless

関連する問題