If N is greater than or equal to 5% of the total documents in the collection, $sample performs a collection scan, performs a sort, and then select the top N documents. As such, the $sample stage is subject to the sort memory restrictions.
If N is less than 5% of the total documents in the collection,
If using WiredTiger Storage Engine, $sample uses a pseudo-random cursor over the collection to sample N documents.
If using MMAPv1 Storage Engine, $sample uses the _id index to randomly select N documents.
Pipeline stages have a limit of 100 megabytes of RAM. If a stage
exceeds this limit, MongoDB will produce an error. To allow for the
handling of large datasets, use the allowDiskUse option to enable
aggregation pipeline stages to write data to temporary files.
原来问题出在存储引擎缓存上。我使用的是一个EC2实例,它导致了OOM错误。我已经能够通过分配如下较小的缓存大小来解决此问题:
你问这个问题的原因是你不知道^{} 运算符是如何工作的。如documentation中所述
所以我认为你想得到的随机文档数大于5%。您需要的是将
allowDiskUse
设置为True
。在您应该将
allowDiskUse
值设置为true
。例如:你可以阅读更多关于这个here。在
相关问题 更多 >
编程相关推荐