Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Now here is how I would approach it. This is based on RaccoonBlog PostComments</p> <p>I would store each users events in a separate document (i.e. UserEvent in the example below) with the user having an additional property linking to it along with a number of events and a timestamp of the last event associated with the user. This would keep the user document much smaller but having alot of the important information</p> <p>In UserEvent it would be a simple document holding id, link to the userid this document references, a "event" collection, and a lasteventid. This way each "event" becomes a sub document for maintenance if needed.</p> <p>Lastly a Index on UserEvent that allows you to query the data easily</p> <pre><code>public class User { public string Id { get; set; } // other user properties public string UserEventId { get; set; } public int NumberOfEvents { get; set; } public DateTimeOffset LastEvent { get; set; } } public class UserEvent { public string Id { get; set; } public string UserId { get; set; } public int LastEventId { get; set; } public ICollection&lt;Event&gt; Events { get; protected set; } public int GenerateEventId() { return ++LastEventId; } public class Event { public int Id { get; set; } public DateTimeOffset CreatedAt { get; set; } public string ActivityType { get; set; } // other event properties } } public class UserEvents_CreationDate : AbstractIndexCreationTask&lt;UserEvent, UserEvents_CreationDate.ReduceResult&gt; { public UserEvents_CreationDate() { Map = userEvents =&gt; from userEvent in userEvents from evt in userEvent.Events select new { evt.CreatedAt, EventId = evt.Id, UserEventId = userEvent.Id, userEvent.UserId, evt.ActivityType }; Store(x =&gt; x.CreatedAt, FieldStorage.Yes); Store(x =&gt; x.EventId, FieldStorage.Yes); Store(x =&gt; x.UserEventId, FieldStorage.Yes); Store(x =&gt; x.UserId, FieldStorage.Yes); Store(x =&gt; x.ActivityType, FieldStorage.Yes); } public class ReduceResult { public DateTimeOffset CreatedAt { get; set; } public int EventId { get; set; } public string UserEventId { get; set; } public string UserId { get; set; } public string ActivityType { get; set; } } } public static class Helpers { public static DateTimeOffset AsMinutes(this DateTimeOffset self) { return new DateTimeOffset(self.Year, self.Month, self.Day, self.Hour, self.Minute, 0, 0, self.Offset); } public static IList&lt;Tuple&lt;UserEvents_CreationDate.ReduceResult, User&gt;&gt; QueryForRecentEvents( this IDocumentSession documentSession, Func &lt;IRavenQueryable&lt;UserEvents_CreationDate.ReduceResult&gt;, IQueryable&lt;UserEvents_CreationDate.ReduceResult&gt; &gt; processQuery) { IRavenQueryable&lt;UserEvents_CreationDate.ReduceResult&gt; query = documentSession .Query&lt;UserEvents_CreationDate.ReduceResult, UserEvents_CreationDate&gt;() .Include(comment =&gt; comment.UserEventId) .Include(comment =&gt; comment.UserId) .OrderByDescending(x =&gt; x.CreatedAt) .Where(x =&gt; x.CreatedAt &lt; DateTimeOffset.Now.AsMinutes()) .AsProjection&lt;UserEvents_CreationDate.ReduceResult&gt;(); List&lt;UserEvents_CreationDate.ReduceResult&gt; list = processQuery(query).ToList(); return (from identifier in list let user = documentSession.Load&lt;User&gt;(identifier.UserId) select Tuple.Create(identifier, user)) .ToList(); } } </code></pre> <p>Then all you have to do to query is something like.</p> <pre><code> documentSession.QueryForRecentEvents(q =&gt; q.Where(x =&gt; x.UserId == user.Id &amp;&amp; x.ActivityType == "asfd").Take(20)).Select(x =&gt; x.Item1); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload