Sub document operations using .net SDK ( c#)

Please let me know if its possible to delete an element from the children array based on the Id from the below sample document using KV api in C#
i.e. I need to delete all the details of Id=100( delete the entire element in the children array)

	"results": [{
		"tutorial": {
			"type": "contact",
			"title": "Mr.",
			"fname": "Ian",
			"lname": "Taylor",
			"age": 56,
			"email": "",
			"children": [{
					"Id": "100",
					"Details": [{
						"fname": "Abama",
						"age": 17,
						"gender": "F"

					"Id": "101",
					"Details": [{
						"fname": "Alex",
						"age": 17,
						"gender": "M"

			"hobbies": [
			"relation": "cousin"


The sub-doc API can only delete items in an array by index. If you need to delete by Id, you have a few options.

  1. Get the whole document and set it back
  2. Get the whole document, determine the index, and then use that for a sub-doc mutation
  3. Get the whole array using a sub-doc get, remove the item, and set the whole array back using a sub-doc mutation
  4. Get the whole array using a sub-doc get, determine the index, and then use that for a sub-doc mutation
  5. Use a N1QL UPDATE query

For 1-4, you’ll probably want to use the CAS value to provide isolation.

@btburnett3 .
Thank you for the reply . I am able to achieve it using N1QL query but was trying to see if the same is achievable using API as N1QL performance is slow when compared to that of .net sdk API. Also I have to explore more CAS value which I am unaware currently.


Options 1-4 should all be faster than N1QL, if it’s worth the effort to you. Here is some documentation on CAS: Concurrent Document Mutations | Couchbase Docs


Thank you for the reply .Sub doc Operation should be performed for all the documents in couchbase is my requirement and approx doc count is estimated to be around 40000-45000.
Options suggested by you should be done for one document at a time and will it be still faster than N1QL ?
As With the help of single N1QL the required operations can be performed for all the docs in the couchbase.


If you’re trying to perform the same mutation to that many documents, then N1QL may be the better approach. Most of the N1QL overhead is involved in starting the operation (query parsing, etc), for bulk operation of that magnitude it’s more inconsequential.

The only thing to beware of is query run time. Each query node can, by default, only execute one query at a time. If the query takes a very long time to complete it can cause degradation in other operations that use queries. If this is an issue, I’d recommend breaking the query into smaller, faster batches.

1 Like

@krishnaa -

You want want to use a N1QL query that simply returns the keys of the docs you want to modify and then use the K/V API to do the actual modifications as you iterate through the keys.