Reputation: 609
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
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
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