我知道include
-RavenDB 的功能。它允许我在数据库的一次往返中立即获取引用的文档。但我的问题是:我首先获取的文档不包含对“其他”文档的引用。但“其他”文档引用了当前文档。
想象一下我们在世界各地都有站点的设置。每个site
可能会触发各种警报。每个alarm
有一个参考site
via siteId
.
现在我想获取所有站点的列表,包括所有警报。但看起来,RavenDB 不可能做到这一点?自从include
只接受“路径”site
- 保存引用文档的 id(或 id 数组)的文档。
这可以通过提供一系列来解决alarmIds
内site'-document and referencing this array in
include. But in contrast to a lot of examples featuring stuff like an
orderwith
订单项where the order is a self contained thing, my
site` 将运行数年,收集 0 到 100 万个警报。这对我来说似乎是个坏主意。
当然我可以反过来:查询所有警报并include
网站通过sitesId
。但这不会返回零警报的站点。
那么这只是我这边的设计错误吗?我误解了什么吗?或者是否无法在一个查询中执行此操作并阻止“n+1 查询”?
public class A
{
public string Id { get; set; }
}
public class B
{
public string Id { get; set; }
public string A { get; set; }
}
public class MultiMapIndex : AbstractMultiMapIndexCreationTask<MultiMapIndex.Result>
{
public class Result
{
public string Id { get; set; }
public IEnumerable<string> Bs { get; set; }
}
public MultiMapIndex()
{
AddMap<A>(items => from a in items
select new Result {Id = a.Id, Bs = new string[0]});
AddMap<B>(items => from b in items
select new Result {Id = b.A, Bs = new[] {b.Id}});
Reduce = results => from result in results
group result by result.Id
into g
select new Result {Id = g.Key, Bs = g.SelectMany(r => r.Bs)};
}
}
[Fact]
public async Task TestCase()
{
using var store = GetDocumentStore();
await new MultiMapIndex().ExecuteAsync(store);
using (var session = store.OpenAsyncSession())
{
await session.StoreAsync(new B {A = "a/1"}, "b/0");
await session.StoreAsync(new A(), "a/1");
await session.StoreAsync(new A(), "a/2");
await session.SaveChangesAsync();
}
WaitForIndexing(store);
using (var session = store.OpenAsyncSession())
{
var results = await session.Query<MultiMapIndex.Result, MultiMapIndex>()
.Include(r => r.Bs)
.ToArrayAsync();
var before = session.Advanced.NumberOfRequests;
var bs = session.LoadAsync<B>(results[0].Bs);
Assert.Equal(before, session.Advanced.NumberOfRequests);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)