当前位置:网站首页>Ravendb starts -- document metadata

Ravendb starts -- document metadata

2022-07-06 21:06:00 Uncle meow

We can store any data content in the document , For example, in the order document, we will store the order status 、 Order item quantity 、 Order amount and so on . But we also need to store some content unrelated to the order document , For example, who modified the order document 、 When did you modify the order document , It's time to Document Metadata ( Document metadata , Let's translate it like this for the time being ) Debut .

Metadata What is stored by default

Metadata The storage format of is the same as the document itself Json,RavenDB Use Metadata Store several important information about tracking documents :

  1. Collection name , Stored in @collection in , How can this attribute determine which set the data document is stored in , If this value is not set , Data documents will be stored in @empty Collection ;
  2. Last modification date of the document , Stored in @last-modified Properties of the , When storing format UTC;
  3. Client type , This is a Key , We can go through this Key Know the type of client , Common types are shown in the following table :

type

explain

Raven-Clr-Type

.NET client

Raven-Java-Class

Java client

Raven-Python-Class

Python client

Customize Metadata Property naming conventions

Besides using RavenDB Built in Metadata In addition to attributes, we can also customize Metadata attribute , For example, we need to record who last modified the order document , Then we can customize Metadata attribute Last-Modified-By-User, The code is as follows :

using Raven.Client.Documents;

var store = new DocumentStore
{
    Urls = new[] { "http://localhost:8080" },
    Database = "Tasks"
};
store.Initialize();
using (var session = store.OpenSession())
{
    var order = session.Load<Order>("orders/1-A");
    // The server will not be requested again , Because when we query data documents ,
    //Metadata  It will also be returned to the client together 
    var metadata = session.Advanced.GetMetadataFor(order);
    metadata["Last-Modified-By-User"] = " Zhang San ";
    session.SaveChanges();
}

We are RavenDB Studio View in orders/1-A The data content , We can see the custom one Metdata Attribute already exists with Metadata The node is down , Here's the picture .

Generally speaking, we seldom use this form to operate in actual development Metadata , We will use events to operate , This will be explained in detail in the later column , Here you only need to know what we are talking about now .

TIP: When we're in RavenDB See in the document with @ At the beginning Metadata Attribute , It means that this attribute is RavenDB Reserved for your own use , So we are expanding Metadata You can't use the same attribute name when using attributes , We customize Metadata Attributes need to follow Pascal nomenclature (PascalCase) perhaps Pascal-Case Nomenclature ( The words that make up the attribute name are capitalized , Use... Between words - Division of no. ), Of course, you can also not follow this suggestion .

原网站

版权声明
本文为[Uncle meow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131128556188.html

随机推荐