解释为什么mongo的结果以相反的ObjectId顺序返回?

时间:2018-10-11 13:33:09

标签: mongodb pymongo

我有要为实体加上标签的新闻文章项目列表和主题标签。

我的查询

db["fmetadata"].find({'$and': [{'$text': {'$search': 'apple trump'}}, {'$or': 
[{'entities': {'$elemMatch': {'$regex': 'apple|trump'}}}, {'tags': {'$elemMatch': {'$regex': 'apple|trump'}}}]}]}).explain()

查询计划

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "dfabric.fmetadata",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "$or" : [
                                                {
                                                        "entities" : {
                                                                "$elemMatch" : {
                                                                        "$regex" : "apple|trump"
                                                                }
                                                        }
                                                },
                                                {
                                                        "tags" : {
                                                                "$elemMatch" : {
                                                                        "$regex" : "apple|trump"
                                                                }
                                                        }
                                                }
                                        ]
                                },
                                {
                                        "$text" : {
                                                "$search" : "apple trump",
                                                "$language" : "english",
                                                "$caseSensitive" : false,
                                                "$diacriticSensitive" : false
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$or" : [
                                        {
                                                "entities" : {
                                                        "$elemMatch" : {
                                                                "$regex" : "apple|trump"
                                                        }
                                                }
                                        },
                                        {
                                                "tags" : {
                                                        "$elemMatch" : {
                                                                "$regex" : "apple|trump"
                                                        }
                                                }
                                        }
                                ]
                        },
                        "inputStage" : {      
                "stage" : "TEXT",
                                "indexPrefix" : {

                                },
                                "indexName" : "title_text_tags_text_entities_text",
                                "parsedTextQuery" : {
                                        "terms" : [
                                                "appl",
                                                "trump"
                                        ],
                                        "negatedTerms" : [ ],
                                        "phrases" : [ ],                                  
                    "negatedPhrases" : [ ]
                                },
                                "textIndexVersion" : 3,
                                "inputStage" : {
                                        "stage" : "TEXT_MATCH",
                                        "inputStage" : {
                                                "stage" : "FETCH",
                                                "inputStage" : {
                                                        "stage" : "OR",
                                                        "inputStages" : [
                                                                {
                                                                        "stage" : "IXSCAN",
                                                                        "keyPattern" : {
                                                                                "_fts" : "text",
                                                                                "_ftsx" : 1
                                                                        },
                                                                        "indexName" : "title_text_tags_text_entities_text",
                                                                        "isMultiKey" : true,
                                                                        "isUnique" : false,
                                                                        "isSparse" : false,
                                                                        "isPartial" : false,
                                                                        "indexVersion" : 2,
                                                                        "direction" : "backward",
                                                                        "indexBounds" : {

                                                                        }
                                                                },
                                                                {
                                                                        "stage" : "IXSCAN",
                                                                        "keyPattern" : {
                                                                                "_fts" : "text",
                                                                                "_ftsx" : 1
                                                                        },
                                                                        "indexName" : "title_text_tags_text_entities_text",
                                                                        "isMultiKey" : true,
                                                                        "isUnique" : false,
                                                                        "isSparse" : false,
                                                                        "isPartial" : false,
                                                                        "indexVersion" : 2,
                                                                        "direction" : "backward",
                                                                        "indexBounds" : {

                                                                        }
                                                                }
                                                        ]
                                                }
                                        }
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "fabric-dev",
                "port" : 27017,
                "version" : "4.0.2",
                "gitVersion" : "fc1573ba18aee42f97a3bb13b67af7d837826b47"
        },
        "ok" : 1
}

我看到了

["queryPlanner"]["winningPlan"]["inputStage"]["inputStage"]["inputStages"]

"stage": "IXSCAN"
"direction": "backward"

请解释为什么?

我正在使用> lastId和limit技术开发分页游标。但是,由于结果是向后返回的,因此我不得不使用

如果我不按自然顺序对结果进行排序,是否可以保证它总是向后/反向?

编辑:如以下评论中所述 我的目标是直观了解为什么向后扫描索引-这是我制定查询的方式吗?还是完全其他的东西?顺序-向前或向后无关紧要,因为它始终保持一致性-总是向前或反之亦然

1 个答案:

答案 0 :(得分:0)

我在stackoverflow上遇到了这个问题,我相信接受的答案以及下面的评论令人满意地给了我我想要的直觉。

How does MongoDB sort records when no sort order is specified?