lasseeskildsen
lasseeskildsen

Reputation: 609

Flattening af collection to properties on document in RavenDB

I have some documents in RavenDB with a collection of attributes, which I would like to flatten using an index.

The document structure is something similar to:

{ 
"Id": "test/1",
"Name": "Some name",
"Attributes": [
{ "Key": "FirstAttr", "Value": "FirstValue" },
{ "Key": "SecondAttr", "Value": "SecondValue" }
]}

My desired output is:

{
"Id": "test/1",
"Name": "Some name",
"FirstAttr": "FirstValue",
"SecondAttr": "SecondValue"
}

Is this possible in RavenDB?

Thanks a bunch!

Upvotes: 1

Views: 191

Answers (2)

lasseeskildsen
lasseeskildsen

Reputation: 609

Ok - answering my own question so others can benefit:

The CreateField method seems to be what I need in the map part in my index:

from t in docs.Test 
select new {
t.Id,
t.Name,
_ = t.Attributes.Select(x => this.CreateField(x.Key, x.Value, true, true))
}

Upvotes: 1

Keith
Keith

Reputation: 21244

Can you do this during the map-reduce?

Map = docs =>
   from doc in docs
   select new {
      Id = doc.Id,
      Name = doc.Name,
      FirstAttr = (doc.Attributes.ContainsKey("FirstAttr") ? doc.Attributes["FirstAttr"] : null,
      SecondAttr = (doc.Attributes.ContainsKey("SecondAttr") ? doc.Attributes["SecondAttr"] : null
   };

Upvotes: 0

Related Questions