バージョン8.2の新機能。
定義
構文
このステージの構文は、次のとおりです。
{ $score: { score: <expression>, normalization: "none|sigmoid|minMaxScaler", weight: <expression> } }
フィールド
$score
は、次のフィールドがあります。
フィールド | タイプ | 説明 | ||
---|---|---|---|---|
| 式 | Computes a new value from the input scores and stores the value in the | ||
| 文字列 | Optional. Normalizes the score to the range of
| ||
| Double | Optional. Number to multiply the |
動作
The output documents from a $score
are the same as the input documents, but includes additional computed score as metadata.
If you specify multiple $score
stages in the pipeline, the last $score
stage in the pipeline overrides the score metadata from previous $score
stages.
例
Consider the following documents in a collection named articles
:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }, { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 } { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 } { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
The following query uses the:
$match
stage to filter the documents where theviews
field value is greater than or equal to1000
.$score
stage to compute a new score that is normalized using$sigmoid
expression and then multiplied by the specified weight,1
.
db.articles.aggregate([ { $match: { views: { $gte: 1000 } } }, { $score: { score: "$score", normalization: "sigmoid", weight: 1 } }, { "$project": { "_id": 0, "author": 1, "views": 1, "score": 1, "calculatedScore": { $meta: "score" } } } ])
[ { author: 'ahn', score: 60, views: 1000, calculatedScore: 1 }, { author: 'li', score: 55, views: 5000, calculatedScore: 1 }, { author: 'ty', score: 95, views: 1000, calculatedScore: 1 } ]