RavenDB Multimap索引 - 不在两个集合中的文档

时间:2016-10-19 02:51:00

标签: ravendb

我知道如何在SQL世界中做到这一点,但试图在RavenDB中解决这个问题。

我有2个课程:

public class Client
{
  public string Id {get;set;}
  public string Name {get;set;}
}

public class WaitingListEntry
{
  public string Id {get;set;}
  public string ClientId {get;set;}
  public string OtherDocumentInformation {get;set;}
}

一个相当简单的地图/减少:

AddMap<Client>(clients => from c in clients select new { Id = (string)null, ClientId = c.ClientId, Name = c.Name });

AddMap<WaitingListEntry>(wls => from wl in wls select new { Id = wl.Id, ClientId = wl.ClientId, Name = (string)null });

Reduce = results => from result in results
  group result by result.ClientId
  into g
  select new { Id = g.Select(x => x.Id).Where(x => x != null).First(),
    ClientId = g.Key,
    Name = g.Select(x => x.Name).Where(x => x != null).First()
  };

我遇到的皱纹是并非所有客户都在等待名单中,我想从索引中排除这些客户。我习惯于SQL思考,无法弄清楚要做什么来实现这一点。

1 个答案:

答案 0 :(得分:1)

您不需要创建multimap index。使用下面的Index,您可以获得客户的所有等待名单:

Index

你会得到结果:

enter image description here

我已经使用过这些数据:

session.Store(new Client() { Id = "client/1", Name = "Client 1" });
session.Store(new Client() { Id = "client/2", Name = "Client 2" });
session.Store(new Client() { Id = "client/3", Name = "Client 3" });

session.Store(new WaitingListEntry() { Id = "waitingListEntry/1", ClientId = "client/1", OtherDocumentInformation = "Info" });
session.Store(new WaitingListEntry() { Id = "waitingListEntry/2", ClientId = "client/2", OtherDocumentInformation = "Info" });

session.SaveChanges();