は次のmongoコレクション "イベント" を考えてみましょう:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), userId: 1, type: "music" eventNum: 1 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), userId: 1, type: "music" eventNum: 2 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b257"), userId: 2, type: "music" eventNum: 3 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b258"), userId: 2, type: "music" eventNum: 4 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), userId: 1, type: "music" eventNum: 5 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), userId: 1, type: "athletic" eventNum: 6 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), userId: 2, type: "athletic" eventNum: 7 }
各イベントはuserId
、type
とeventNum
で作成されます。私はuserId: 1
の上位3イベントを見つける必要があります。だから私はこのクエリを実行します。
db.getCollection('events').aggregate([
{
"$match": {
"userId": 1
}
},
{
"$sort": { "eventNum": 1 }
},
{
"$limit": 3
}
])
データセットを返します(注そこには「運動」のイベントが返されません):今すぐ
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), userId: 1, type: "music" eventNum: 1 }
{ "_id" : ObjectId("512bc962e835e68f199c8687"), userId: 1, type: "music" eventNum: 2 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), userId: 1, type: "music" eventNum: 5 }
、しかし、私は、userId: 1
のすべての「運動」イベントをしたいです
db.getCollection('events').aggregate([
{
"$match": {
"userId": 1
}
},
{
"$sort": { "eventTime": 1 }
},
{
"$limit": 3
},
{
"$match": {
"type": "athletic"
}
}
])
しかし、このクエリは実際にこのデータを返しますが、トップ3には「運動」のイベントが存在しないので、彼らがトップ3である場合にのみ、私たちは何の書類を返さないために、次のクエリを期待します設定:
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), userId: 1, type: "athletic" eventNum: 6 }
誰かがここで何が起こっているのか説明できますか? 2回目の試合後にソート/リミットが起こっているようです。複数のクエリを実行せずにこれを回避する方法はありますか?
これは本当に例を必要としているが、それは一般的にあなたが間違っていることを指します。 '$ match'条件の前に返された文書を表示します。 –
問題を説明するためのより良い例を追加しました。 – M1Reeder
[パイプラインシーケンス最適化](https://docs.mongodb.com/manual/core/aggregation-pipeline-optimization/#pipeline-sequence-optimization)以外は何も考えられていません。私はこの事実のために "背中の背後にある*"ことが起こるのが好きではありません。 –