本文介绍了MongoDB Facet Error 管道需要文本分数元数据,但没有可用的文本分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 mongodb 分面管道,以下是其子集:

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.

如果您对此有任何想法,请不要犹豫,分享

谢谢!

推荐答案

此问题已在 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: ... }
])

这篇关于MongoDB Facet Error 管道需要文本分数元数据,但没有可用的文本分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:29