Extracting meta data in RavenDB

Viewed 113

Similarly to this question in SQL Server, I want to query for metadata, but in ravenDB.

The ideal output table would be

Column Name | Primary Key | Data Type | Size | Nullability 
1 Answers

You can get the "schema" for a document using a query like so:

declare function Schema(doc) {
    var fields = [];
    var keys = Object.keys(doc);
    for(var i = 0; i < keys.length; i ++){
        fields.push({
            Name: keys[i],
            Type: typeof doc[keys[i]]
        })
    }
    return { Fields: fields };
}
from Employees  as e
select Schema(e)
limit 1

This will give you JSON that looks like this:

{
    "Fields": [
        {
            "Name": "LastName",
            "Type": "string"
        },
        {
            "Name": "FirstName",
            "Type": "string"
        },
        {
            "Name": "Title",
            "Type": "string"
        },
        {
            "Name": "Address",
            "Type": "object"
        },
        {
            "Name": "HiredAt",
            "Type": "string"
        },
        {
            "Name": "Birthday",
            "Type": "string"
        },
        {
            "Name": "HomePhone",
            "Type": "string"
        },
        {
            "Name": "Extension",
            "Type": "string"
        },
        {
            "Name": "ReportsTo",
            "Type": "string"
        },
        {
            "Name": "Notes",
            "Type": "object"
        },
        {
            "Name": "Territories",
            "Type": "object"
        },
        {
            "Name": "@metadata",
            "Type": "object"
        },
        {
            "Name": "New",
            "Type": "string"
        }
    ],
    "@metadata": {
        "@flags": "HasRevisions, HasAttachments, Reverted",
        "@id": "employees/8-A",
        "@last-modified": "2019-10-23T15:03:03.0396651Z",
        "@change-vector": "A:2239343-ZAAsE6haIECPXQA6XsIt5A, A:1887-0N64iiIdYUKcO+yq1V0cPA, A:6214-xwmnvG1KBkSNXfl7/0yJ1A",
        "@projection": true,
        "@index-score": 0
    }
}

Note that in RavenDB, you don't really have a schema. Different docs may have different fields. Depending on the complexity of your requirements, you may need to recurse into nested objects, like Territories or Address.

Related