Thinking Sites
Thinking Sites

Reputation: 3542

Querying dynamic properties in RavenDB

I'm having some trouble querying dynamic properties in Raven.

Here are my classes:

    public class Parent
    {
        public string ID { get; set; }
        public dynamic Child { get; set; }
    }

    public class Son
    {
        public int A { get; set; }
        public Guid Key { get; set; }
    }

    public class Daughter
    {
        public int A { get; set; }
        public Guid Key { get; set; }
    }

What I'm trying to do is index parents whose child is a son, but not a daughter, and index by the child properties 'A' or 'Key'. In my case, the children can't inherit from an interface because the real child classes will look entirely different from each other. These classes are built for a test that matches the scenario.

The index I've built so far looks like this:

    public class DynamicIndex : AbstractIndexCreationTask
    {
        public override Raven.Abstractions.Indexing.IndexDefinition CreateIndexDefinition()
        {
            return new IndexDefinition()
            {
                Map = @"from doc in docs.Parent select new { A = doc.Dynamic.A, B = doc.Dynamic.B }"
            };
        }
    }

This index works except I cannot seem to filter out children who match one type and not the other. The serialized JSON for Child contains a property called '$type' which is the full name of the Type. My instinct tells me to use this to differentiate between child types but I don't know how to access it.

How can I expand my index's map to include the '$test' property or is there another way to filter for children matching a particular type?

Upvotes: 3

Views: 343

Answers (1)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Map = @"from doc in docs.Parent select new { A = doc.Dynamic.A, B = doc.Dynamic.B, Type = doc.Dynamic[""$type""] }"

Should work

Upvotes: 1

Related Questions