在上一节当中已经介绍了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. 清理kafka zookeeper

    ; ; /; rm -rf /app/zookeeper/logs/*; rm -rf /app/pet_kafka_emds2_cluster/kafka-logs/*; rm -rf /app/p ...

  2. 更改MySQL数据库目录位置[zz]

    MYSQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步:1.home目录下建立data目录cd /homemkdir data2.把My ...

  3. Axure chrome 扩展显示已损坏的解决方法

    下载地址 链接:https://pan.baidu.com/s/11K3t_mvgJg51siO_jNRejg 提取码:goz1 如果链接失效,请留言或站内信提醒我更新 疑问 之前用的好好的Axure ...

  4. Spring MVC+Hibernate JPA搭建的博客系统项目中所遇到的坑

    标签: springmvc hibernate 2016年12月21日 21:48:035133人阅读 评论(0) 收藏 举报  分类: Spring/Spring MVC(6)  Hibernate ...

  5. Windows 虚拟桌面工具 Desktops v2.0

    https://technet.microsoft.com/en-us/sysinternals/cc817881.aspx

  6. OVAL学习笔记

                很多其它好文章:http://blog.csdn.net/aap159951/article/details/51131937        OVAL由MITRE公司开发.是一 ...

  7. Dynamic Control Flow in ML

    https://arxiv.org/abs/1805.01772 https://www.leiphone.com/news/201702/cb7cPOtzFj1pgRpk.html

  8. Asp.net Core 项目API接口服务器部署

    Windows server 2008服务器部署: DotNetCore.1.0.0.RC2-WindowsHosting 或者DotNetCore.1.0.5_1.1.2-WindowsHostin ...

  9. [Windows Azure] Getting Started with Windows Azure SQL Data Sync

    Getting Started with Windows Azure SQL Data Sync In this tutorial, you learn the fundamentals of Win ...

  10. [Windows Azure] How to Create and Deploy a Cloud Service?

    The Windows Azure Management Portal provides two ways for you to create and deploy a cloud service: ...