MongoDB Facet Error pipeline requires text score metadata, but there is no text score available

让人想犯罪 __ 提交于 2021-02-11 13:05:42

问题


I created a mongodb faceted pipeline that the following is a subset of:

    db.books.aggregate( [
    {
        $facet: {
            "categories": [
                {
                    $match: {
                        $text: { $search: "Pattern" }
                    }
                }, 
                {
                    $group: {
                        _id: "$Category",
                        count: {
                            $sum: 1
                        }
                    }
                },
                {
                    $sort: {
                        "count": -1
                    }
                },
                {
                    $project: {
                        "score": { "$meta": "textScore"},
                        "Category": "$_id",
                        "_id": 0,
                        "count": 1
                    }
                },
                {
                    $limit: 10
                }
            ]
        }
    }
])

Two other output fields exist in the pipeline aside from category but resemble the structure outlined in the pipeline above. Whenever I run this pipeline I get the error: "pipeline requires text score metadata, but there is no text score available"

This error only happens when using a facet pipeline. Running each pipeline stage individually works perfectly.

If you have any thoughts around this, please don't hesitate to share

Thanks!


回答1:


This has been answered in groups.google.com/forum/#!topic/mongodb-user/Amozaj74prI

In summary, what was needed was the match stage to appear at the beginning of the pipeline instead of it being in every facet stage:

db.books.aggregate( [
    { $match: {$text: {$search: "Pattern"}}},
    { $addFields: {score: {$meta: "textScore"}}},
    { $facet: ... }
])


来源:https://stackoverflow.com/questions/52953662/mongodb-facet-error-pipeline-requires-text-score-metadata-but-there-is-no-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!