Patch Request: Remove using AllPositions

44 views
Skip to first unread message

Anders Granberg

unread,
Jul 24, 2016, 5:51:08 AM7/24/16
to RavenDB - 2nd generation document database
Hi,

I'm trying to remove all comments from a blog post using a patch request. As far as I can understand you can use the AllPositions = true to affect all items in a collection. My code looks like this:

store.DatabaseCommands.Patch("BlogPosts/1", new[] {

 new PatchRequest() {

   Type = PatchCommandType.Remove,

   AllPositions = true,

   Name = "Comments"

 }

});


But when running this code I get the following exception:

Server Error in '/' Application.

Url: "/databases/RavenLab/bulk_docs"

System.InvalidOperationException: Cannot remove value from  'Comments' because position element does not exists or not an integer and no value was present
  at Raven.Database.Json.JsonPatcher.RemoveValue(PatchRequest patchCmd, String propName, RavenJToken token) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Json\JsonPatcher.cs:line 165
  at Raven.Database.Json.JsonPatcher.Apply(PatchRequest patchCmd) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Json\JsonPatcher.cs:line 37
  at Raven.Database.Json.JsonPatcher.Apply(PatchRequest[] patch) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Json\JsonPatcher.cs:line 30
  at Raven.Database.Actions.PatchActions.ApplyPatchInternal(String docId, Etag etag, TransactionInformation transactionInformation, Func`2 patcher, Func`1 patcherIfMissing, Func`1 getDocsCreatedInPatch, Func`1 getDebugActions, Boolean debugMode, Boolean skipPatchIfEtagMismatch, String[] participatingIds) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Actions\PatchActions.cs:line 107
  at Raven.Database.Actions.PatchActions.ApplyPatch(String docId, Etag etag, PatchRequest[] patchExistingDoc, PatchRequest[] patchDefaultDoc, RavenJObject defaultMetadata, TransactionInformation transactionInformation, Boolean debugMode, Boolean skipPatchIfEtagMismatch, String[] participatingIds) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Actions\PatchActions.cs:line 45
  at Raven.Database.Extensions.CommandExtensions.Execute(ICommandData self, DocumentDatabase database, BatchResult batchResult, String[] participatingIds) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Extensions\CommandExtensions.cs:line 67
  at Raven.Database.Extensions.CommandExtensions.ExecuteBatch(ICommandData self, DocumentDatabase database, String[] participatingIds) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Extensions\CommandExtensions.cs:line 27
  at Raven.Database.DocumentDatabase.ProcessBatch(IList`1 commands, CancellationToken token) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\DocumentDatabase.cs:line 1214
  at Raven.Database.DocumentDatabase.<>c__DisplayClass63.<BatchWithRetriesOnConcurrencyErrorsAndNoTransactionMerging>b__60(IStorageActionsAccessor _) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\DocumentDatabase.cs:line 1119
  at Raven.Storage.Esent.TransactionalStorage.ExecuteBatch(Action`1 action, EsentTransactionContext transactionContext) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Storage\Esent\TransactionalStorage.cs:line 845
  at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Storage\Esent\TransactionalStorage.cs:line 806
  at Raven.Database.DocumentDatabase.BatchWithRetriesOnConcurrencyErrorsAndNoTransactionMerging(IList`1 commands, CancellationToken token) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\DocumentDatabase.cs:line 1120
  at Raven.Database.DocumentDatabase.Batch(IList`1 commands, CancellationToken token) in c:\Builds\RavenDB-Stable-3.0\Raven.Database\DocumentDatabase.cs:line 655
  at Raven.Database.Server.Controllers.DocumentsBatchController.<BulkPost>d__8.MoveNext() in c:\Builds\RavenDB-Stable-3.0\Raven.Database\Server\Controllers\DocumentsBatchController.cs:line 0
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()


What am I doing wrong or can you simply not use AllPositions property with remove this way?

Oren Eini (Ayende Rahien)

unread,
Jul 24, 2016, 6:07:50 AM7/24/16
to ravendb
We don't support it like that.
Set the value to an empty array

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Anders Granberg

unread,
Jul 24, 2016, 12:05:26 PM7/24/16
to RavenDB - 2nd generation document database
Oh ok, that's a very simple yet effective solution though. Thanks.

I also have another scenario where I want to remove two different comments from the blog post. I'm using the position property to specify which comments to remove. See below:

store.DatabaseCommands.Batch(

new[] {

 new PatchCommandData {

  Key = "BlogPosts/1",

  Patches = new[] {

  new PatchRequest {

   Type = PatchCommandType.Remove,

   Name = "Comments",

   Position = 2

  },

  new PatchRequest {

   Type = PatchCommandType.Remove,

   Name = "Comments",

   Position = 3

  }

 }

}

});


I'm getting an index out of range exception when running this code. Probably because the last remove patch is no longer a valid position since the array has been modified. What is the correct approach when removing more than one post in a single batch?

Thanks

Daniel Häfele

unread,
Jul 24, 2016, 1:39:04 PM7/24/16
to RavenDB - 2nd generation document database
You could remove them if you do it in reverse.
E. g. start with the last position.

Oren Eini (Ayende Rahien)

unread,
Jul 24, 2016, 2:23:23 PM7/24/16
to ravendb
Or use scripted patching, which is much better for this kind of thing

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


On Sun, Jul 24, 2016 at 6:58 PM, Anders Granberg <granber...@gmail.com> wrote:
Oh ok, well that's a very simple yet effective solution though. Thanks.

I have another scenario regarding patching and removing. In this case I want to remove two different comments. The only way I know is by using the Position attribute. See my code below:


store.DatabaseCommands.Batch(

  new[] {

new PatchCommandData {


                                                       Key = "BlogPosts/1",


                                                       Patches = new[] {


                                                                           new PatchRequest {


                                                                                                Type = PatchCommandType.Remove,


                                                                                                Name = "Comments",


                                                                                                Position = 2


                                                                                            },


                                                                           new PatchRequest {


                                                                                                Type = PatchCommandType.Remove,


                                                                                                Name = "Comments",


                                                                                                Position = 3


                                                                                            }


                                                                       }


                                                   }


                          });



On Sunday, July 24, 2016 at 12:07:50 PM UTC+2, Oren Eini wrote:

Anders Granberg

unread,
Jul 25, 2016, 12:16:03 AM7/25/16
to RavenDB - 2nd generation document database
Scripted patching works great. For some reason it slipped my mind. Thanks Oren!
Reply all
Reply to author
Forward
0 new messages