SODA for In-Database JavaScriptを使用したコレクションのデータ・ガイドの取得
データ・ガイドは、一連のJSON文書に含まれる構造および型情報の概要を示します。これらの文書内で使用されているフィールドに関するメタデータを記録します。JSONドキュメントに対する優れたインサイトを提供し、データ・セットの概要把握に役立ちます。
SodaCollection.getDataGuide()を使用してデータ・ガイドを作成できます。SODAでデータ・ガイドを取得するには、コレクションがJSONのみであり、"dataguide"オプションが"on"であるJSON検索索引を持っている必要があります。データ・ガイドは、sodaCollection.getDataGuide()からSodaDocumentのJSONコンテンツとして戻されます。データ・ガイドは現在のコレクションの状態から推測されます。コレクションが大きくなったり、ドキュメントが変更されると、その後にgetDataGuide()がコールされるたびに新しいデータ・ガイドが戻されます。
例8-21 コレクションのデータ・ガイドの生成
この例では、メソッドgetDataGuide()を使用してコレクションemployeesCollection (例8-8で作成)のデータ・ガイドを取得し、メソッドgetContentAsString()を使用してコンテンツを文字列として出力します。
export function createDataGuide(){
// open the collection
const col = soda.openCollection('employeesCollection');
if(col === null){
throw new Error("'employeesCollection' does not exist");
}
// generate a Data Guide (requires the Data Guide index)
const doc = col.getDataGuide();
console.log(doc.getContentAsString());
}
データ・ガイドでは、すべてのフィールドとそのデータ型を含む、コレクションに関する興味深いインサイトを提供できます。employeesCollectionのデータ・ガイドは、この章の読者にとってはすでになじみ深い内容である可能性がありますが、不明なJSONドキュメントをこの方法で簡単に分析できます。前のコード・ブロックでは、次のデータ・ガイドが画面に出力されます:
{
"type": "object",
"o:length": 1,
"properties": {
"_id": {
"type": "id",
"o:length": 24,
"o:preferred_column_name": "DATA$_id"
},
"email": {
"type": "string",
"o:length": 16,
"o:preferred_column_name": "DATA$email"
},
"jobId": {
"type": "string",
"o:length": 16,
"o:preferred_column_name": "DATA$jobId"
},
"salary": {
"type": "number",
"o:length": 8,
"o:preferred_column_name": "DATA$salary"
},
"hireDate": {
"type": "string",
"o:length": 32,
"o:preferred_column_name": "DATA$hireDate"
},
"lastName": {
"type": "string",
"o:length": 16,
"o:preferred_column_name": "DATA$lastName"
},
"firstName": {
"type": "string",
"o:length": 16,
"o:preferred_column_name": "DATA$firstName"
},
"managerId": {
"type": "string",
"o:length": 4,
"o:preferred_column_name": "DATA$managerId"
},
"employeeId": {
"type": "number",
"o:length": 4,
"o:preferred_column_name": "DATA$employeeId"
},
"phoneNumber": {
"type": "string",
"o:length": 16,
"o:preferred_column_name": "DATA$phoneNumber"
},
"departmentId": {
"type": "string",
"o:length": 4,
"o:preferred_column_name": "DATA$departmentId"
},
"commissionPct": {
"type": "string",
"o:length": 32,
"o:preferred_column_name": "DATA$commissionPct"
}
}
}