使用RavenDB v4.2或更高版本,我想设置一个查询另一个集合的索引。基本上,在索引的映射部分中重现WHERE IN子句。
下面的模型代表两个集合。这里每个User
都有设备ID的集合:
class Device {
public string Id { get; set; }
public string Name { get; set; }
}
class User {
public string Id { get; set; }
public string BlogPostId { get; set; }
public List<string> DeviceIds { get; set; }
}
现在考虑以下索引作为我要实现的示例:
public class DeviceIndex : AbstractIndexCreationTask<Device, DeviceIndex.Result>
{
public class Result
{
public string Id { get; set; }
public string DeviceName { get; set; }
public bool HasUser { get; set; }
public int UserCount { get; set; }
}
public DeviceIndex()
{
Map = devices => from d in devices
select new Result
{
Id = d.Id,
DeviceName = d.Name,
HasUser = ... ?, // How to get this from Users collection?
UserCount = ... ? // same...
};
}
如何在此索引中填充HasUser
true / false和UserCount
属性?例如。如何在此处查询“用户”集合?
[请注意,为简洁起见,此示例已被认真简化。我对变通办法或更改其背后的逻辑不是很感兴趣。
如果您在Device类List<string> Users
中有一个用户列表,一个包含Users集合中的文档ID的列表,则可以为这些相关文档编制索引。
请参阅:https://demo.ravendb.net/demos/csharp/related-documents/index-related-documents
或者相反,在Users集合上创建索引,并为相关的[[Device info
建立索引