【问题标题】:RavenDB Multimap index - documents not in both collectionsRavenDB Multimap 索引 - 不在两个集合中的文档
【发布时间】:2016-10-20 00:16:06
【问题描述】:

我知道如何在 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;}
}

还有一个相当简单的 map/reduce:

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 思维,不知道在这里做什么来完成这个。

【问题讨论】:

    标签: ravendb


    【解决方案1】:

    您无需创建multimap index。使用下面的Index,您可以与客户一起获取所有等候名单:

    你会得到结果:

    我已经使用了这些数据:

    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();
    

    【讨论】:

    • 天哪,我是个傻瓜。我在我的变压器中做 LoadDocument,不知道我可以在 Map 中做这件事。谢谢!
    • 虽然您可以使用 LoadDocument 来索引相关文档,但还是建议创建一个 map/reduce 索引。例如,“使用 LoadDocument 将加载的文档添加到跟踪列表中。这可能会导致进行非常昂贵的计算,尤其是在多个文档跟踪同一个文档时”。
    猜你喜欢
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    相关资源
    最近更新 更多