RavenDB - retrieving part of document

前端 未结 3 1975
深忆病人
深忆病人 2021-01-04 02:02

I am playing with Raven DB for few days and I would like to use it as a storage for my Web chat application. I have document which contains some user data and chat history -

3条回答
  •  走了就别回头了
    2021-01-04 02:52

    From what I've seen you can do this (based on the original "User" scenario above):

    public class UserSummary
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }
    

    Then you can do this:

    documentSession.Query().AsProjection();
    

    Looking at the Raven server it spits this out as part of the query:

    ?query=&pageSize=128&fetch=Name&fetch=Email&fetch=Id
    

    So it looks like it is querying and returning only a subset of the original object, which is good.

    This also works:

    documentSession.Query().Select( x=> new User { Name = x.Name, Email= x.Email })
    

    But I don't think that is as clean as returning a UserSummary object.

    Some follow up questions to those who have posted responses:

    The link to RaccoonBlog has this example:

    https://github.com/ayende/RaccoonBlog/blob/master/RaccoonBlog.Web/Infrastructure/Indexes/PostComments_CreationDate.cs

    Would that method be preferred over the .AsProjection()? What is the difference between the two approaches?

提交回复
热议问题