$type - Amazon DocumentDB

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

$type

$type산자는 문서에서 필드의 데이터 유형을 확인하는 데 사용됩니다. 유형별 작업 또는 검증이 필요할 때 사용할 수 있습니다. 연$type산자는 평가된 표현식의 BSON 유형을 반환합니다. 반환된 유형은 필드 또는 표현식의 유형에 해당하는 문자열입니다.

플래너 버전 2.0에에 대한 인덱스 지원이 추가되었습니다$type.

파라미터

  • expression : 평가할 표현식입니다.

예제(MongoDB 쉘)

샘플 문서 생성

db.documents.insertMany([ { _id: 1, name: "John", age: 30, email: "john@example.com" }, { _id: 2, name: "Jane", age: "25", email: 123456 }, { _id: 3, name: 123, age: true, email: null } ]);

쿼리 예제

db.documents.find({ $or: [ { age: { $type: "number" } }, { email: { $type: "string" } }, { name: { $type: "string" } } ] })

출력

[ { "_id": 1, "name": "John", "age": 30, "email": "john@example.com" }, { "_id": 2, "name": "Jane", "age": "25", "email": 123456 } ]

이 쿼리는 age 필드가 "number" 유형이고, email 필드가 "string" 유형이고, name 필드가 "string" 유형인 문서를 반환합니다.

코드 예제

$type 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function findByType() { 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('documents'); const results = await collection.find({ $or: [ { age: { $type: 'number' } }, { email: { $type: 'string' } }, { name: { $type: 'string' } } ] }).toArray(); console.log(results); client.close(); } findByType();
Python
from pymongo import MongoClient def find_by_type(): 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['documents'] results = list(collection.find({ '$or': [ {'age': {'$type': 'number'}}, {'email': {'$type': 'string'}}, {'name': {'$type': 'string'}} ] })) print(results) client.close() find_by_type()