在上一节当中已经介绍了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. 安卓listview滚动时背景变黑的解决方法

    ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...

  2. IRGAN:A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models

    https://arxiv.org/pdf/1705.10513.pdf 论文阅读笔记: https://www.cnblogs.com/liaohuiqiang/p/9694277.html htt ...

  3. 【解决】网站运行一段时间后就无法访问,重启Tomcat才能恢复

    [背景]重新部署了网站,运行环境是Linux(centOS6.5)+Tomcat7+mysql5,作了相关安全配置和系统优化工作: [问题]运行几天后,出现了网站无法访问的现象,具体是进入网站首页时页 ...

  4. wifiphisher 钓鱼工具的使用

    wifiphisher 钓鱼工具的使用一.简介 Wifiphisher是一个安全工具,具有安装快速.自动化搭建的优点,利用它搭建起来的网络钓鱼攻击WiFi可以轻松获得密码和其他凭证.与其它(网络钓鱼) ...

  5. groupby elasticsearch

    GET usertag/usertag/_search { "query": { "match": { "tagname": "春 ...

  6. RabbitMQ 消费端 Client CPU 100%的解决办法

    Func<bool> run = () => { try { using (IConnection conn = cf.CreateConnection()) { using (IM ...

  7. andorid 直接解压后的xml的解密

    1.首先可以去看看这个gitHub: https://github.com/tracer0tong/axmlprinter 2.把apk.py 和 axmlprinter.py下载下来. 2.1(如果 ...

  8. 解决sweetalert 无故报错 elem.className.replace Uncaught TypeError: Cannot read property 'className' of null

    今天碰到这么一个问题,在使用sweetalert的时候时有时无会报错 elem.className.replace Uncaught TypeError: Cannot read property ' ...

  9. Unity预计算全局光照的学习(速度优化,LightProbe,LPPV)

    1.基本参数与使用 1.1 常规介绍 使用预计算光照需要在Window/Lighting面板下找到预计算光照选项,保持勾选预计算光照并保证场景中有一个光照静态的物体 此时在编辑器内构建后,预计算光照开 ...

  10. DDR3控制

    很简单的,app_en和app_rdy一握手,代表MIG接受了一个写数据请求或者读数据请求,只要保证app_en和app_rdy握手,根本就不关心写数据rdy,这是MIG的一个bug,你看它源码就知道 ...