Delete item from array?

Hello, I am using NET SDK 2.7.
Is there a way to delete single item from array field?

Eg.
Doc = { "some_property" = [ "a" , "b", "c" ] }
How to delete item "a" in "some_property" ? Or only full round trip - get property and update?

Thanks!

Hi @Cyb,

There is a subdocument API for .NET available that allows you to make changes to portions of documents. However, I don’t think there is an “array remove” feature in subdocuments (yet).

Some options you might want to explore:
a) Full round trip (as you’ve suggested). If this has a strong adverse effect on performance or latency of your app, you may want to consider splitting out that array into a separate document.
b) You can use a N1QL UPDATE query to modify a portion of your document without making a full round trip. Check out those docs and/or check out the N1QL forum here.

Another option that probably won’t help or work for your situation, but I thought I’d mention just in case is to use a data structure, specifically a List. This gives you an abstraction with which you can remove an item from a CouchbaseList in .NET just like a regular List in .NET. Behind the scenes it still uses the subdocument API, so again, this will probably require a full round-trip. However, if the subdocument operations are added in the future, then you’ll likely just need to update your SDK to receive the performance benefits.

I believe the subdocument API can remove an element from an array using the Remove command on IMutateInBuilder, however only by index not by value.

For example, this would remove the first element in the array:

builder.Remove("some_property[0]").Execute()
1 Like

Hi, @matthew.groves ,

Almost all of my operations with db are done with subdoc api and I was hoping may be there is a way to remove item “subdoc” style and I just missed it. It seems like I have to move to N1QL queries (which I tried to escape) :slight_smile:

Anyway, thanks for suggestions!

Hi, @btburnett3

Unfortunately I can’t use this. The order of items is not constant.
But thanks for reply!

Cool, I learned something new :slight_smile:

1 Like

It’s just a path syntax https://docs.couchbase.com/dotnet-sdk/2.7/subdocument-operations.html#path-syntax

:slight_smile: :slight_smile: :slight_smile:

N1QL itself would do this with a full document operation with CAS. So in effect, you can do the same thing from the SDK if appropriate. N1QL is nice and expressive for this kind of thing though, but at the end of the day the operational complexity is the same or higher as get()ing the doc and then upsert()ing the doc after mutating it.

1 Like

Hmm, @ingenthr, thanks for the tip!

I already have simple implementation like get-modify-update. In this case I think there is no reason for me to check out N1QL docs.

Thanks!

Sure, glad to help. By the way, I certainly don’t want to talk you out of checking out N1QL, which can be very expressive, powerful and concise for data manipulation. I just wanted you to know that at least for this case, N1QL doesn’t have any special runtime tricks to get the work done that you can’t express in C# app logic.