두 필드로 정렬하려고 시도합니다. 우선 순위를 우선으로 지정하려고합니다. 대신에 두 번째 필드 (성)에 대다수의 사례. 때로는 올바르게 작동하지만 변경 사항이 무엇인지 모릅니다.MongoEngine (플라스크)가 올바르게 집계하지 않음 - 매시간마다
동일한 명령이 Robo3t를 클라이언트로 사용하여 동일한 데이터베이스에 연결하면 정상적으로 작동합니다. , 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)
같은 코드 :
아래의 코드로, 플라스크를 실행하고 Mongoengine를 사용하여 MongoDB에
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"}}
)
인덱스 (3.4 0.9) 아래와 같이 찾습니다
> db.speakers.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "website_v3.speakers"
}
]
dB 버전 v3.4.9
사용 0나는 인덱스 (데이터 정렬)를 중요하게 만들려고했지만 심지어 정확히 같은 일이 발생했습니다. 먼저 작동하지만, 그렇지 않습니다.
아이디어를 환영합니다!
는편집 : 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"),
}
필자가 제안한대로'$ match'와'$ sort'를 넣으려고했지만 동일한 결과를 얻습니다. 항목은'lastName'에 의해 정렬되고'importance'에 의해 정렬되지 않습니다. 이상하게도 Robo3T에서는 완벽하게 작동합니다. – gsapu
서버 측에 쿼리를 기록하고 비교해 볼 것을 권장합니다. 만약 플라스크가 그것을 설명 할 수있는 질의를 망친다면. Robo3T와 앱을 사용하여 정확히 동일한 쿼리가 기록 된 경우 문제가 다른 곳에서 발생해야합니다. https://stackoverflow.com/questions/15204341/mongodb-logging-all-queries – dnickless
다른 잠재적 인 혼란의 원인은 데이터 정렬 일 수 있습니다. 설정, 혼합 된 필드 유형 (예 : int 및 중요도 문자열), null 값, 값 시작시 불필요한 공백 문자 또는 필드 이름의 오타. 마지막으로 다른 정렬 방법을 테스트 할 수도 있습니다. 단지'find()'를 사용하거나 다른 두 단계없이 문제를 분리 할 수 있습니다. – dnickless