I have a collection of documents named Actions. Action in db looks like this:
{
"Name": "Name1",
"ActionType": "typeA"
}
I need to migrate all my Actions so ActionType property would contain an array of strings:
{
"Name": "Name1",
"ActionType": ["typeA"]
}
I don't want to create any converters in my project like mentioned here. That should be a simple utility which can be run separately.
The idea is to load all documents, made necessary changes and save them (like here). My current code looks like that but it doesn't work correctly:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Linq;
using Raven.Json.Linq;
namespace RavenDbMigration
{
public class SimpleMigrator
{
private readonly IDocumentStore _documentStore;
public SimpleMigrator(IDocumentStore documentStore)
{
_documentStore = documentStore;
}
public void Run()
{
using (IDocumentSession session = _documentStore.OpenSession())
{
var fullRavenIdsList = new List<string> {"1", "2", "3"};
RavenJObject[] ravenJObjects = session.Load<RavenJObject>(fullRavenIdsList); // Problem here - ravenJObjects contains 3 null values instead of actions
foreach (var ravenJObject in ravenJObjects)
{
var results = ravenJObject["Actions"] as RavenJArray;
if (results != null)
{
foreach (var result in results)
{
if (result != null && result.ContainsKey("ActionType"))
{
var actionTypes = new List<string>();
var actionType = result.Value<string>("ActionType");
if (actionType != null)
{
actionTypes.Add(actionType);
}
result["ActionType"] = new RavenJArray(actionTypes);
}
}
}
}
session.SaveChanges();
}
}
}
}
I use RavenDB.Client of version 2.5.2700.
You can patch your document collection "Actions" either via the Raven Studio or via code.
this.ActionType = [this.ActionType];
This will result in a document looking like this:
"Name": "Name1",
"ActionType": [
"typeA"
]
Hope this helps!