问题
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