RavenDb学习(八)高级特性上半部分
时间:2022-04-29
本文章向大家介绍RavenDb学习(八)高级特性上半部分,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1、事务支持
别的关系型数据库和RavenDb一起使用
using (var transaction = new TransactionScope())
{
BlogPost entity = session.Load<BlogPost>("blogs/1");
entity.Title = "Some new title";
session.SaveChanges(); // will create HTTP request
session.Delete(entity);
session.SaveChanges(); // will create HTTP request
transaction.Complete(); // will commit transaction
}
2、文档元数据
Raven-Clr-Type - Records the CLR type, set and used by the JSON serialization/deserialization process in the Client API.
Raven-Entity-Name - Records the entity name, aka the name of the RavenDB collection this entity belongs to.
Non-Authoritive-Information - This boolean value will be set to true if the data received by the client has been modified by an uncommitted transaction. You can read more on it in the Advanced section.
Temp-Index-Score - When querying RavenDB, this value is the Lucene score of the entity for the query that was executed.
Raven-Read-Only - This document should be considered read only and not modified.
Last-Modified - The last modified time-stamp for the entity.
@etag - Every document in RavenDB has a corresponding e-tag (entity tag) stored as a sequential Guid. This e-tag is updated by RavenDB every time the document is changed.
@id - The entity id, as extracted from the entity itself.
获取最后修改时间的例子:
var product = session.Load<Product>(1);
RavenJObject metadata = session.Advanced.GetMetadataFor(product);
// Get the last modified time stamp, which is known to be of type DateTime
DateTime collectionName = metadata.Value<DateTime>("Last-Modified");
返回所有表的表名的map函数:
from doc in docs
where doc["@metadata"]["Raven-Entity-Name"] != null
select new { Tag = doc["@metadata"]["Raven-Entity-Name"] };
//修改元数据
1)通过session.Advanced.GetMetadatFor(entity) 获取元数据,然后修改并保存
2)通过documentStore.RegisterListener(myStoreListener) 注册一个IDocumentStoreListener,当有session要进行保存的时候,它能修改元数据
3、查询指定列
分页查询某个列
var firstPage = session.Advanced.DocumentStore.DatabaseCommands.GetTerms("indexName", "MyProperty", null, 128);
var secondPage = session.Advanced.DocumentStore.DatabaseCommands.GetTerms("indexName", "MyProperty", firstPage.Last(), 128);
4、动态列
public class Product
{
public string Id { get; set; }
public List<Attribute> Attributes { get; set; }
}
public class Attribute
{
public string Name { get; set; }
public string Value { get; set; }
}
上述例子,一个产品有很多个属性,属性的类型是不固定的,需要是dynamic的
//创建索引
public class Product_ByAttribute : AbstractIndexCreationTask<Product>
{
public Product_ByAttribute()
{
Map = products => from p in products
select new
{
_ = p.Attributes
.Select(attribute =>
CreateField(attribute.Name, attribute.Value, false, true))
};
}
}
//我们对值并不关注,只是希望调用CreateField方法,所以用_作为一种转换反射。
//查询
var products = session.Advanced.LuceneQuery<Product>("Product/ByAttribute")
.WhereEquals("Color", "Red")
.ToList();
这样子它不仅仅支持字符串,也支持数字等
5、自定义序列化
当RavenDb收到一个POCO的时候,它会默认把它序列化为JSON格式的数据。
1)忽略一个属性
public class Carrot
{
public string Id { get; set; }
public decimal Length { get; set; }
[JsonIgnore]
public decimal LengthInInch
{
get
{
/* some calculations */
return this.Length;
}
set
{
//...
}
}
}
2)序列化的时候更换名字
public class Recipe
{
public string Id { get; set; }
[JsonProperty(PropertyName = "dishes")]
public IList<IVegetable> SideDishes { get; set; }
}
3)允许自引用
//树形结构数据
[JsonObject(IsReference = true)]
public class Category
{
public string Id { get; set; }
public string Name { get; set; }
public Category Parent { get; set; }
public List<Category> Children { get; set; }
public Category()
{
this.Children = new List<Category>();
}
public void Add(Category category)
{
category.Parent = this;
this.Children.Add(category);
}
}
4)自定义IContractResolver实现是序列
store.Conventions.JsonContractResolver = new DefaultContractResolver(shareCache: true)
{
DefaultMembersSearchFlags =
BindingFlags.Public | BindingFlags.Instance
};
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【猫狗数据集】利用tensorboard可视化训练和测试过程
- springmvc之视图解析流程
- 【猫狗数据集】从命令行接收参数
- django-URL重定向(八)
- Jetpack新成员,App Startup一篇就懂
- python之利用魔术方法实现自己定义的二维向量
- 【猫狗数据集】使用top1和top5准确率衡量模型
- django-URL反向解析Reverse(九)
- spring之如何在web应用中使用?
- 【猫狗数据集】使用预训练的resnet18模型
- django-URL应用命名空间(十)
- 【colab pytorch】模型权重初始化
- 【colab pytorch】提取模型中的某一层
- django-URL实例命名空间(十一)
- 【colab pytorch】数据预处理