前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RavenDb学习(二)简单的增删查改

RavenDb学习(二)简单的增删查改

作者头像
岑玉海
发布2018-03-01 15:06:05
1.1K0
发布2018-03-01 15:06:05
举报
文章被收录于专栏:岑玉海岑玉海岑玉海
在上一节当中已经介绍了RavenDb的文档设计模式,这一节我们要具体讲一讲如何使用api去访问RavenDb

1.连接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.
2.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);



3.查询

//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 >= 10)

    .ToList();



//分页查询

var results = session.Query<BlogPost>()

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

    .Take(10) // 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(10)

    .ToArray();

var totalResults = stats.TotalResults;



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

RavenQueryStatistics stats;

// get the first page

var results = session.Query<BlogPost>()

    .Statistics(out stats)

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

    .Take(10) // 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((1 * 10) + skippedResults) // retrieve results for the second page, taking into account skipped results

    .Take(10) // 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 > 10)

    .Customize(x => x.WaitForNonStaleResultsAsOf(new DateTime(2011, 5, 1, 10, 0, 0, 0)))

    .ToArray();



//设置查询返回最后一次更新 documentStore.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档