Skip to content

@chaliy /RavenDb.cs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
RavenDb demo for Kiev ALT.NET
// Kiev ALT.NET: NoSQL
// Install RavenDb from http://ravendb.net/ , run server
// Reference RavenDB from NuGet
// Compile and execute this code
// Enjoy
using System;
using System.Linq;
using Raven.Client.Document;
using Raven.Client.Indexes;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Connection..");
var db = new DocumentStore
{
Url = "http://localhost:8080/"
};
db.Initialize();
Console.WriteLine("First doc...");
using(var session = db.OpenSession())
{
session.Store(new SaleRecord());
session.SaveChanges();
}
Console.WriteLine("Update doc..");
using(var session = db.OpenSession())
{
var rec = session.Load<SaleRecord>("salerecords/1");
rec.Total = 12.0m;
rec.Date = DateTime.Now;
session.SaveChanges();
}
Console.WriteLine("Delete doc...");
using(var session = db.OpenSession())
{
var rec = session.Load<SaleRecord>("salerecords/1");
session.Delete(rec);
session.SaveChanges();
}
Console.WriteLine("Index, Map/Reduce demo...");
IndexCreation.CreateIndexes(typeof(Program).Assembly, db);
using(var session = db.OpenSession())
{
session.Store(new SaleRecord
{
Total = 10.0m,
Date = new DateTime(2012, 11, 12)
});
session.Store(new SaleRecord
{
Total = 13.0m,
Date = new DateTime(2012, 11, 13)
});
session.Store(new SaleRecord
{
Total = 12.0m,
Date = new DateTime(2012, 11, 13)
});
session.SaveChanges();
}
}
}
public class SaleRecord
{
public DateTime Date {get; set;}
public decimal Total {get; set;}
}
public class SaleRecords_Daily
: AbstractIndexCreationTask<SaleRecord>
{
// http://localhost:8080/indexes/salerecords/daily
public SaleRecords_Daily()
{
Map = docs => from doc in docs
select new
{
Date = doc.Date,
Total = doc.Total
};
Reduce = results => from result in results
group result by result.Date.Date into g
select new
{
Date = g.Key,
Total = g.Sum(x => x.Total)
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.