在上一节当中已经介绍了RavenDb的文档设计模式,这一节我们要具体讲一讲如何使用api去访问RavenDb

.连接RavenDb

var documentStore = new DocumentStore { Url = "http://myravendb.mydomain.com/" };

documentStore.Initialize();

var documentStore = new DocumentStore

{

ConnectionStringName = "MyRavenConStr"

};

在app.config中配置如下:

<connectionStrings>

<add name="Local" connectionString="DataDir = ~\Data"/>

<add name="Server" connectionString="Url = http://localhost:8080"/>

<add name="Secure" connectionString="Url = http://localhost:8080;user=beam;password=up;ResourceManagerId=d5723e19-92ad-4531-adad-8611e6e05c8a"/>

</connectionStrings>

参数:

DataDir - embedded mode的参数, 只能实例化一个EmbeddableDocumentStore,

Url -  server mode的参数

User / Password - server mode的参数

Enlist - whatever RavenDB should enlist in distributed transactions. 不适合Silverlight

ResourceManagerId - 可选的, server mode的参数, the Resource Manager Id that will be used by the Distributed Transaction Coordinator (DTC) service to identify Raven. A custom resource manager id will need to be configured for each Raven server instance when Raven is hosted more than once per machine.不适合Silverlight

Database - server mode的参数,不是用内置的数据库

Url的方式:

Url = http://ravendb.mydomain.com
connect to a remote RavenDB instance at ravendb.mydomain.com, to the default database
Url = http://ravendb.mydomain.com;Database=Northwind
connect to a remote RavenDB instance at ravendb.mydomain.com, to the Northwind database there
Url = http://ravendb.mydomain.com;User=user;Password=secret
connect to a remote RavenDB instance at ravendb.mydomain.com, with the specified credentials
DataDir = ~\App_Data\RavenDB;Enlist=False
use embedded mode with the database located in the App_Data\RavenDB folder, without DTC support.
.Session使用案例
//写入
string companyId;

using (var session = documentStore.OpenSession())

{

    var entity = new Company { Name = "Company" };

    session.Store(entity);

    session.SaveChanges();

    companyId = entity.Id;

}

//读取

using (var session = documentStore.OpenSession())

{

    var entity = session.Load<Company>(companyId);

    Console.WriteLine(entity.Name);

}

//删除,一旦删除无法恢复

using (var session = documentStore.OpenSession())

{

session.Delete(existingBlogPost);

session.SaveChanges();

}

下面两种方式也可以删除 session.Advanced.Defer(new DeleteCommandData { Key = "posts/1234" });

session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null);

.查询

//PageSize

如果没有设置PageSize,客户端调用是一次128条记录,服务端调用是一次1024条记录,远程调用是一次30条记录,可以配置。

RavenDb为了加快查询数据的速度,它在后台使用的是lucene的索引方式,通过linq来生成HTTP RESTful API。

//查询,用linq的方式查询很方便

var results = from blog in session.Query<BlogPost>()

              where blog.Category == "RavenDB"

              select blog;

var results = session.Query<BlogPost>()

    .Where(x => x.Comments.Length >= )

    .ToList();

//分页查询

var results = session.Query<BlogPost>()

    .Skip() // skip 2 pages worth of posts

    .Take() // Take posts in the page size

    .ToArray(); // execute the query

//分页的时候,我们一次取10条,但是我们也要知道总共有多少条数据,我们需要通过TotalResults来获得

RavenQueryStatistics stats;

var results = session.Query<BlogPost>()

    .Statistics(out stats)

    .Where(x => x.Category == "RavenDB")

    .Take()

    .ToArray();

var totalResults = stats.TotalResults;

//跳过指定的临时的数据集,每次查询都记录下上一次查询记录的跳过的查询记录,该值保存在SkippedResults

RavenQueryStatistics stats;

// get the first page

var results = session.Query<BlogPost>()

    .Statistics(out stats)

    .Skip( * ) // retrieve results for the first page

    .Take() // page size is 10

    .Where(x => x.Category == "RavenDB")

    .Distinct()

    .ToArray();

var totalResults = stats.TotalResults;

var skippedResults = stats.SkippedResults;

// get the second page

results = session.Query<BlogPost>()

    .Statistics(out stats)

    .Skip(( * ) + skippedResults) // retrieve results for the second page, taking into account skipped results

    .Take() // page size is 10

    .Where(x => x.Category == "RavenDB")

    .Distinct()

    .ToArray();

//查询出来的数据不一定是最新的,如果stats.IsStale不为true的话,它就报错的啦

if (stats.IsStale)

{

    // Results are known to be stale

}

//设定获取时间,更新时间截止到某个时刻

RavenQueryStatistics stats;

var results = session.Query<Product>()

    .Statistics(out stats)

    .Where(x => x.Price > )

    .Customize(x => x.WaitForNonStaleResultsAsOf(, , , , , , )))

    .ToArray();

//设置查询返回最后一次更新 documentStore.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;

RavenDb学习(二)简单的增删查改的更多相关文章

  1. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

  2. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

  3. mybatis实现简单的增删查改

    接触一个新技术,首先去了解它的一些基本概念,这项技术用在什么方面的.这样学习起来,方向性也会更强一些.我对于mybatis的理解是,它是一个封装了JDBC的java框架.所能实现的功能是对数据库进行增 ...

  4. MySQL学习-入门语句以及增删查改

    1. SQL入门语句 SQL,指结构化查询语言,全称是 Structured Query Language,是一种 ANSI(American National Standards Institute ...

  5. EF简单的增删查改

    Add /// <summary> /// /// </summary> public void Add() { TestDBEntities2 testdb = new Te ...

  6. MySQL学习笔记1(增删查改)

    创建表: /* 创建数据库 create database 数据库名; */ CREATE DATABASE mybase; /* 使用数据库 use 数据库名 */ USE mybase; /* 创 ...

  7. asp.net MVC最简单的增删查改!(详)

    折腾了两天搞出来,但原理性的东西还不是很懂,废话不多说上图上代码 然后右键models,新建一个数据模型 注意我添加命名为lianxi 添加后如上 接下来在controllers添加控制器还有在Vie ...

  8. 一般处理程序+htm C#l简单的增删查改

    首先引用两个文件一个dll: 数据库表已创建 首先编写数据读取部分 /// <summary> /// 查询 /// </summary> /// <param name ...

  9. Hibernate 的事物简单的增删查改

    Hibernate 是一个优秀的ORM框架体现在: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管 ...

随机推荐

  1. Java 8 Streams filter examples

    1. Streams filter() and collect() package com.mkyong.java8; import java.util.Arrays;import java.util ...

  2. 下载必要的android sdk

    做android自动化测试,必须要下载anroid sdk 下载后最少安装方法:

  3. Latex中如何设置字体颜色(3种方式)

    Latex中如何设置字体颜色(三种方式)   1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/ma ...

  4. mysql性能测试(索引)

    首先,使用Talend随机生成一千万条数据: 数据库表中现在有1千万+的数据: mysql> select count(*) from zhangchao; +----------+ | cou ...

  5. 如何在Visual Studio VS中定义多项目模板

    https://msdn.microsoft.com/en-us/library/ms185308.aspx Multi-project templates act as containers for ...

  6. MSSQL分组取后每一组的最新一条记录

    数据库中二张表,用户表和奖金记录表,奖金记录表中一个用户有多条信息,有一个生效时间,现在要查询: 奖金生效时间在三天前,每个用户取最新一条奖金记录,且用户末锁定 以前用的方法是直接写在C#代码中的: ...

  7. [svc][dhcp]关于无线ap自动发现选项dhcp option43配置

    有人问到option43的问题,查了点资料.总结了下win和linux的option43的配置: windows2003 http://www.h3c.com.cn/Service/Channel_S ...

  8. java 字符串格式化

    转:http://blog.sina.com.cn/s/blog_af26e3330101988v.html 1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化 ...

  9. 菜鸟学Java(六)——简单验证码生成(Java版)

    验证码大家都知道,它的作用也不用我多说了吧.如果不太清楚请参见百度百科中的解释,一般验证码的生成就是随机产生字符(数字.字母或者汉字等),然后将这些生成的字符绘制成一张图片,再在图片上加上一些干扰元素 ...

  10. PBR Metallic/Roughness工作流中Albedo与F0的计算方法

    首先简单回顾一下典型的纯金属与绝缘体的PBR属性: 纯金属: Albedo(diff): 0 F0(spec): >0.3 (or 0.5, epic/allegorithmic etc.) M ...