Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created May 25, 2011 08:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwarren/990623 to your computer and use it in GitHub Desktop.
Save mattwarren/990623 to your computer and use it in GitHub Desktop.
Array patching with RavenDB
using (var store = NewDocumentStore())
{
using (var s = store.OpenSession())
{
s.Store(new Post
{
//Create an empty list that we can add items to later
Comments = new List<Comment>()
});
s.SaveChanges();
}
store.DatabaseCommands.Batch(
new[] {
new PatchCommandData
{
Key = "posts/1",
Patches = new[]
{
new PatchRequest
{
Type = PatchCommandType.Add,
Name = "Comments",
Value = RavenJObject.FromObject(new Comment {AuthorId = "authors/456"})
},
new PatchRequest
{
Type = PatchCommandType.Add,
Name = "Comments",
Value = RavenJObject.FromObject(new Comment {AuthorId = "authors/789"})
},
new PatchRequest
{
Type = PatchCommandType.Add,
Name = "Comments",
Value = RavenJObject.FromObject(new Comment {AuthorId = "authors/101112"})
},
}
}
});
using (var s = store.OpenSession())
{
var comments = s.Load<Post>("posts/1").Comments;
Assert.Equal(3, comments.Count);
Assert.Equal("authors/456", comments[0].AuthorId);
Assert.Equal("authors/789", comments[1].AuthorId);
Assert.Equal("authors/101112", comments[2].AuthorId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment