Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:23:09+00:00 2026-06-03T17:23:09+00:00

I have 2 different object types stored in RavenDb, which are a parent/child type

  • 0

I have 2 different object types stored in RavenDb, which are a parent/child type relationship, like this in JSON:

Account/1
{        
    "Name": "Acc1",
}

Items/1
{
    "Account": "Account/1",
    "Value" : "100",
    "Tags": [
       "tag1",
       "tag2"]
}

Items/2
{
    "Account": "Account/1",
    "Value" : "50",
    "Tags": [
       "tag2"]
}

Note that I don’t want to store these in the same document, as an account may have thousands of items.

I am trying to write a map/reduce index that will return me something like:

{
    "Account": "Acc1",
    "TagInfo": [
        { "TagName" : "tag1",
          "Count" : "1",  //Count of all the "tag1" occurrences for acc1
          "Value" : "100" //Sum of all the Values for acc1 which are tagged 'tag1'
        },
        { "TagName" : "tag2",
          "Count" : "2",  //Two items are tagged "tag2"
          "Value" : "150"
        }]
}

i.e. a list of all the distinct tag names along with the number of each and their value.

I think I need to use a multi-map to map the Account and Items collections together, but I can’t figure out the reduce part to create the “TagInfo” part of the result.

Is this possible, or am I modelling this all wrong in Raven?

EDIT:

The class I want to retrieve from this query would look something like this:

public class QueryResult
{
    public string AccountId {get;set;}
    public TagInfo Tags {get;set;} 
}

public class TagInfo
{
    public string TagName {get;set;}
    public int Count {get;set;}
    public int TotalSum {get;set;}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:23:12+00:00Added an answer on June 3, 2026 at 5:23 pm

    OK, so I figured out a way to do this in an acceptable manner that builds on Daniel’s answer, so I’ll record it here for any future travellers (probably myself!).

    I changed from trying to return one result per account, to one result per account/tag combination, so the index had to change as follows (note the group by in the reduce is on 2 properties):

    public class TagsWithCountAndValues : AbstractIndexCreationTask<Item, TagsWithCountAndValues.ReduceResult>
    {
        public class ReduceResult
        {
            public string AccountId { get; set; }
            public string AccountName { get; set; }
            public string TagName { get; set; }
            public int TagCount { get; set; }
            public int TagValue { get; set; }
        }
    
        public TagsWithCountAndValues()
        {
            Map = items => from item in items
                           from tag in item.Tags
                           select new ReduceResult
                           {
                               AccountId = item.AccountId,
                               TagName = tag,
                               TagCount = 1,
                               TagValue = item.Value
                           };
    
            Reduce = results => from result in results
                                where result.TagName != null
                                group result by new {result.AccountId, result.TagName}
                                into g
                                select new ReduceResult
                                           {
                                               AccountId = g.Key.AccountId,
                                               TagName = g.Key.TagName,
                                               TagCount = g.Sum(x => x.TagCount),
                                               TagValue = g.Sum(x => x.TagValue),
                                           };
    
            TransformResults = (database, results) => from result in results
                                                      let account = database.Load<Account>(result.AccountId)
                                                      select new ReduceResult
                                                                 {
                                                                     AccountId = result.AccountId,
                                                                     AccountName = account.Name,
                                                                     TagName = result.TagName,
                                                                     TagCount = result.TagCount,
                                                                     TagValue = result.TagValue,
                                                                 };
        }
    }
    

    As before, querying this is just:

    var results = session
        .Query<TagsWithCountAndValues.ReduceResult, TagsWithCountAndValues>()
        .ToList();
    

    The result of this can then be transformed into the object I originally wanted by an in-memory LINQ query. At this point the number of results that could be returned would be relatively small, so performing this at the client end is easily acceptable. The LINQ statement is:

    var hierachicalResult = from result in results
                            group new {result.TagName, result.TagValue} by result.AccountName
                            into g
                            select new
                            {
                                Account = g.Key,
                                TagInfo = g.Select(x => new { x.TagName, x.TagValue, x.TagCount })
                            };
    

    Which gives us one object per account, with a child list of TagInfo objects – one for each unique tag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project which exposes object model to use by different types of
I have an object which will be stored in two different data structures. I
I have beans which have Objects which can contain different types. Now when I
I have a base class object array into which I have typecasted many different
I'd like to have multiple versions of an object with different access modifiers on
I have an application that has many different types of objects that each persist
I have an array of different type objects and I use a BinaryWriter to
I want to push many objects into a array and each object have different
I have an object with different values that is name,nameid, lifebeging,lifeEndiging .... etc, for
I need to create an array using a object using different format/structure I have:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.