UNSET from .netClient

Hi,

i was trying to execute an unset from the dotnet client, but i cann’t find anything in the documentation talking about UNSET statements executed from the sdk, instead of any console. What im trying to do, is to to remove a property from all documents fulfilling a given condition. The statement works perfect from the console, makes what it has to do:
$"Update myBucket UNSET {propertyToRemove} Where category = 154 ";

can somebody please, please tell me how cann i execute that update from the .netClient? I was tryint to find something like
var cluster = new Cluster(clientConfiguration);
var authenticator = new PasswordAuthenticator(_couchbaseConnectionString.UserName,
_couchbaseConnectionString.Password);
cluster.Authenticate(authenticator);
_bucket = cluster.OpenBucket(_bucketName);
_bucket.Execute($"Update myBucket UNSET {propertyToRemove} Where category = 154 ");

Another way would be to load all documents and remove each property manually, but it makes absolutely no sense for me because they can be tons of documents, just the load of everyone would mean transmitting gigabytes through the network, and i have to avoid that.

Thanks in advance,

Sebastian

Hi Sebasitan

There isn’t any additional stuff you have to do to execute UNSET commands through the .NET SDK. For example, the following worked for me:

var config = new ClientConfiguration
{
	Servers = new List<Uri>
	{
		new Uri("couchbase://10.112.193.101")
	}
};

var cluster = new Cluster(config);
cluster.Authenticate("Administrator", "password");
var bucket = cluster.OpenBucket("default");

bucket.Upsert("user:mike", new { name = "mike", gender = "male", type = "user"});
bucket.Query<dynamic>("UPDATE default UNSET gender WHERE type = 'user'");

Although, I see you are executing a query using bucket.Exectute - what version of the .NET SDK are you using?

@detky

You have a couple of different options to accomplish what you’re trying to do.

Your first option is pretty close to the example you have written, except for a few details. First, you always want to create the Cluster object as a singleton for the lifetime of your application. Second, I believe you want to use QueryAsync rather than Execute to run the query.

The advantage of this approach is that you don’t need to know the primary key of your documents. The disadvantage of this approach is that you may have problems with timeouts if this query is mutating too many documents.

A second approach, if you know the primary keys of the documents, would be to use the sub-doc API. This API will communicate directly with data nodes, avoiding query processing, but allows you to manipulate the document without sending the entire document over the wire and serializing/deserializing it.

foreach (var key in keys)
{
    // You could also parallelize this loop for better performance
    await _bucket.MutateIn(key).Remove(propertyToRemove).ExecuteAsync();
}
2 Likes

Hallo Mike,

thanks for answering
:blush: The code i posted earlier, was just for giving you an idea of what I’m trying to do (I’m searching
for a kind of “Execute” method). My problem is that in the sdk (latest .net client) I can’t find nothing like that, and when I try to execute the statement using bucket.Query, I get the error “error: 3000 “input was not a statement”, but the same statement
executed from the web console works fine.

Regards,

Sebastian

Hallo Brant,

thanks for answering
:blush: And also for pointing me to the MutateIn function, it looks to be a better solution for my problem.
Unfortunately I can’t try it right now (vpn down), but I’ll let you know

Regards,

Sebastian

In addition to Mike and Brant’s excellent answers…

Note that the error 3000 here is returned from the query service itself. My guess is that you have some kind of issue in string substitution you may be doing, since I see a little bit of evidence of that in the post.

I’d probably recommend you put a small example together and turn up the log levels so you can see what the conversation is at a lower level. That’d probably make it a bit more clear what the difference is between what you’re entering in the console is and what your string argument to the .Query() is doing.

1 Like