如何提高MongoDB聚合搜索,排序和分页查询的性能?

时间:2019-11-01 12:18:57

标签: mongodb performance aggregation-framework

MongoDB集合中有成千上万的文档,当我运行此查询时,会花费很多时间。

这是我的数据库查询:

db.collection.aggregate( [
{   $match: commonSearch },
{
    $addFields: {
        "ingestionDate": {
            $toDate: "$commonData.bo_ingestionDate"
        },
        "lastModifiedDate": { $toDate: "$commonData.bo_lastModifiedDate" }
    }
},
{
    $project: {
        "commonData": 1, "data.page_1": 1, documentType: 1,
        isExported: 1, dcn: 1,
        endorserNumber: "$data.page_1.data.H_BARCODE.value",
        "receivedDate": {
            "$dateToString": {
                "format": "%Y-%m-%d",
                "date": "$ingestionDate"
            }
        },
        "exportedDate": {
            "$dateToString": {
                "format": "%Y-%m-%d",
                "date": "$lastModifiedDate"
            }
        }
    }
},
{   $match: searchQuery },
{   $sort: { [sortingKey]: sortingOrder } },
{   $skip: (req.body.pageNumber - 1) * req.body.rowPerPage },
{   $limit: req.body.rowPerPage }
] )
.allowDiskUse(true)

查询需要更多的时间来执行;如何减少执行时间。

0 个答案:

没有答案