Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB获取长数组的速度很慢_Javascript_Arrays_Mongodb - Fatal编程技术网

Javascript MongoDB获取长数组的速度很慢

Javascript MongoDB获取长数组的速度很慢,javascript,arrays,mongodb,Javascript,Arrays,Mongodb,所以我们在mongo的数组中有2000到100000个项目的列表。我想把它拔出来用 解释显示它使用索引,只扫描1个文档。但它需要1.5-3秒才能运行。我想是因为阵列很大。有没有办法加快速度?我觉得被困在那里了 这是一个问题 db.monitors.find({userId: "xxxx"}) 下面是解释 { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "xx.monitors",

所以我们在mongo的数组中有2000到100000个项目的列表。我想把它拔出来用

解释显示它使用索引,只扫描1个文档。但它需要1.5-3秒才能运行。我想是因为阵列很大。有没有办法加快速度?我觉得被困在那里了

这是一个问题

db.monitors.find({userId: "xxxx"})
下面是解释

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "xx.monitors",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "userId" : {
                "$eq" : "xxx"
            }
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "userId" : 1
                },
                "indexName" : "user",
                "isMultiKey" : false,
                "direction" : "forward",
                "indexBounds" : {
                    "userId" : [ 
                        "[\"xxx\", \"xxx\"]"
                    ]
                }
            }
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 1,
        "totalDocsExamined" : 1,
        "executionStages" : {
            "stage" : "FETCH",
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 2,
            "advanced" : 1,
            "needTime" : 0,
            "needFetch" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 1,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 2,
                "advanced" : 1,
                "needTime" : 0,
                "needFetch" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "userId" : 1
                },
                "indexName" : "user",
                "isMultiKey" : false,
                "direction" : "forward",
                "indexBounds" : {
                    "userId" : [ 
                        "[\"xxx\", \"xxx\"]"
                    ]
                },
                "keysExamined" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0,
                "matchTested" : 0
            }
        },
        "allPlansExecution" : []
    },
    "serverInfo" : {
        "host" : "xx",
        "port" : 27017,
        "version" : "3.0.4",
        "gitVersion" : "0481c958daeb2969800511e7475dc66986fa9ed5"
    }
}

userId
字段是数组吗?速度会很慢。我记得与MongoDB NYC办公室的一位访客成员进行的一次用户组讨论。在讨论中,一般性的说法是,超过500个元素的阵列的性能会显著下降。如果有包含2000到100000项的数组,则可能应该为这些项使用单独的集合。
monitors.xyz
是数组。因此,
monitors.dog
monitors.animal.
我们的目标是扫描数千篇文章,然后将这些ID添加到人们设置的监视器关键字中。所以有人可能会关心狗,所以他们设置了“狗”这个词。5000篇文章可能有这个词。这些文档是按userid分组的。作为对您的问题的编辑,最好使用注释来提醒更改。然而,与其说“我们之所以不这样做是因为……”,不如用另一种方式实际设置数据并进行基准测试。除非你已经这样做了,并且知道这是缓慢的。