var result = await session.Query<SampleData, DailyPriceListIndex>()
.ProjectFromIndexFieldsInto<DailyPriceListIndex.Result>()
.Where(p => p.Date >= startDate && p.Date < endDate)
.ToListAsync();
Failing test. You need the RDB server and Nodatime bundle installed.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NodaTime;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Tests.Helpers;
using Xunit;
namespace FilterByNodatTime
{
public class IndexTest : RavenTestBase
{
[Fact]
public async Task CanIndexAndQuery()
{
var store = new DocumentStore
{
ConnectionStringName = "RavenDB",
DefaultDatabase = "FilterByNodatTime"
};
store.Initialize(true);
new DailyPriceListIndex().Execute(store);
using (var session = store.OpenAsyncSession())
{
for (int i = 0; i < 2; i++)
{
await session.StoreAsync(new SampleData
{
ADay = new LocalDate(2015, 3, 20),
Prices = GeneratePrices()
});
}
await session.SaveChangesAsync();
}
var startDate = new LocalDate(2015, 3, 24);
var endDate = startDate.PlusDays(2);
using (var session = store.OpenAsyncSession())
{
var result = await session.Query<SampleData, DailyPriceListIndex>()
.ProjectFromIndexFieldsInto<DailyPriceListIndex.Result>()
.Where(p => p.Date >= startDate && p.Date < endDate)
.ToListAsync();
Assert.NotEmpty(result);
}
}
private static Dictionary<LocalDate, decimal?> GeneratePrices()
{
var prices = new Dictionary<LocalDate, decimal?>();
var startDate = new LocalDate(2015, 3, 20);
for (int i = 0; i < 5; i++)
{
prices.Add(startDate.PlusDays(i), 100);
}
return prices;
}
}
public class SampleData
{
public Dictionary<LocalDate, decimal?> Prices { get; set; }
public LocalDate ADay { get; set; }
}
public class DailyPriceListIndex : AbstractIndexCreationTask<SampleData>
{
public DailyPriceListIndex()
{
Map = pricelists => from pricelist in pricelists
from price in pricelist.Prices
select new
{
Date = price.Key,
Price = price.Value
};
StoreAllFields(FieldStorage.Yes);
}
public class Result
{
public LocalDate Date { get; set; }
public decimal? Price { get; set; }
}
}
}
Example of document in RDB. Notice the format of the key.
{
"Prices": {
"den 20 mars 2015": 100,
"den 21 mars 2015": 100,
"den 22 mars 2015": 100,
"den 23 mars 2015": 100,
"den 24 mars 2015": 100,
"den 25 mars 2015": 100,
"den 26 mars 2015": 100,
"den 27 mars 2015": 100,
"den 28 mars 2015": 100,
"den 29 mars 2015": 100
},
"ADay": {
"ticks": 14268096000000000,
"calendar": "ISO"
}
}
Is there a way to get the key to be in the format of ADay instead?
Any suggestions how to handle this case.
This query returns 0 hits.
Failing test. You need the RDB server and Nodatime bundle installed.
Example of document in RDB. Notice the format of the key.
Is there a way to get the key to be in the format of ADay instead?
RavenDB Nodatime Bundle: 3.0.0.891 Packages