$meta - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$meta

$meta 집계 연산자는 집계 파이프라인의 문서와 연결된 메타데이터에 액세스합니다. 일반적으로 텍스트 검색 점수를 검색하고 관련성을 기준으로 결과를 정렬하는 데 사용됩니다.

파라미터

  • textScore: 검색 쿼리와 문서 관련성을 나타내는 텍스트 검색 점수를 검색합니다.

예제(MongoDB 쉘)

다음 예제에서는 집계 파이프라인에서 $meta 연산자를 사용하여 텍스트 검색 점수를 검색하고 정렬하는 방법을 보여줍니다.

샘플 문서 생성

db.articles.createIndex({ content: "text" }); db.articles.insertMany([ { _id: 1, title: "Python Programming", content: "Python is a versatile programming language used for web development." }, { _id: 2, title: "Python Guide", content: "Learn Python programming with Python tutorials and Python examples." }, { _id: 3, title: "Java Basics", content: "Java is another popular programming language." } ]);

쿼리 예제

db.articles.aggregate([ { $match: { $text: { $search: "Python" } } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: -1 } } ]);

출력

[ { _id: 2, title: 'Python Guide', content: 'Learn Python programming with Python tutorials and Python examples.', score: 1.5 }, { _id: 1, title: 'Python Programming', content: 'Python is a versatile programming language used for web development.', score: 0.75 } ]

코드 예제

$meta 집계 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('articles'); const result = await collection.aggregate([ { $match: { $text: { $search: "Python" } } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: -1 } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['articles'] result = list(collection.aggregate([ { '$match': { '$text': { '$search': 'Python' } } }, { '$addFields': { 'score': { '$meta': 'textScore' } } }, { '$sort': { 'score': -1 } } ])) print(result) client.close() example()