Patch Request - Remove from array

327 views
Skip to first unread message

Paul Hinett

unread,
Apr 25, 2012, 1:24:48 PM4/25/12
to rav...@googlegroups.com

How can I do a patch request to remove a certain element from an array, I know you can do it by position but I won’t know what the position is.

 

It’s a simple IList<string>, I know the string to pass in.

 

Paul

Matt Warren

unread,
Apr 25, 2012, 3:40:20 PM4/25/12
to rav...@googlegroups.com
With the current patching API you can't do this, you have to tell it where in the array you want to make the change.

You'll have to Load the doc, modify it in the client and then Store it again after.

Oren Eini (Ayende Rahien)

unread,
Jun 22, 2012, 3:40:12 PM6/22/12
to rav...@googlegroups.com
No, you have to do this manually

On Fri, Jun 22, 2012 at 9:31 PM, Jon Wynveen <jonwy...@gmail.com> wrote:
I assume that would be the case for updating arrays as well, correct? I have a user document with a Roles array on it that I want to update if the role name changes. Do I need to just load each user, find the role, change it, and store it for each user? Or is there a way to use the patching API for this?

Here's my user document:
{
name: "username",
...,
roles: [
{id:"roles/1",name:"role1"},
{id:"roles/2",name:"role2"}
]
}

I'm only just getting started with Raven, so let me know if I'm structuring this wrong.

Maverix

unread,
Jun 28, 2012, 9:47:49 PM6/28/12
to rav...@googlegroups.com
Isn't there a unit test for this in RavenDB?
  [Fact]
  public void RemoveItemFromArrayByValue()
  {
   var patchedDoc = new JsonPatcher(RavenJObject.Parse(@"{ name: ""Joe Doe"", roles: [""first/role"", ""second/role"", ""third/role""] }")).Apply(
    new[]
    {
     new PatchRequest
     {
      Type = PatchCommandType.Remove,
      Name = "roles",
      Value = "second/role"
     },
    });
   Assert.Equal(@"{""name"":""Joe Doe"",""roles"":[""first/role"",""third/role""]}",
       patchedDoc.ToString(Formatting.None));
  }

Oren Eini (Ayende Rahien)

unread,
Jun 29, 2012, 12:29:45 AM6/29/12
to rav...@googlegroups.com
Um,yeah. I forgot about this :-)
Reply all
Reply to author
Forward
0 new messages