![](/img/trans.png)
[英]Flask_Mongoengine: Not a mongoengine object after using aggregate
[英]MongoEngine (flask) does not sort (via aggregate) correctly - every now and then
嘗試按兩個字段進行排序,將優先級放在第一個-但在大多數情況下,似乎優先於第二個字段(lastName)。 有時,它可以正常運行-不確定是什么原因做出了更改。
使用Robo3t作為客戶端,並連接相同的數據庫,該命令可以正常工作。
使用以下代碼運行Flask並使用Mongoengine:
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)
在Robo3t中使用相同的代碼(“ True”變為“ 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.9)上的索引如下所示:
> db.speakers.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "website_v3.speakers"
}
]
使用數據庫版本v3.4.9
我什至嘗試創建重要性索引(排序規則),但是完全一樣。 首先它起作用,然后它不起作用。
任何想法歡迎!
編輯:
好像flask_mongoengine弄亂了$ sort和$ project的順序。
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"),
}
MongoDB將按照您指定的確切順序執行聚合階段(大多數情況下)。 $match
運算符不對輸出順序提供任何保證。
因此,您可能希望將$match
放入管道的開頭(緊隨$sort
),出於性能原因,這是一個好主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.