Update document if not exists?

n1ql

#1

I use N1QL to update my document.

How to know how many document has been updated?

And if I update an document which does not exist, how to check this situation?

I am using Couchbase 4 and Java JDK 2.2


#2

Hello,

The UPDATE statement returns a mutationCount, which should address both of your questions.


#3

Hello geraldss,
Here is my example and I don’t know how to solve my problems
My case is that have a plain query string and I want to know the question as I mentioned at the beginning.

String query = "UPDATE testBucket SET available = 'true' WHERE type = 'blue'";
N1qlQuery qr = N1qlQuery.simple(query);
N1qlQueryResult queryResult = bucket.query(qr);
if(queryResult.parseSuccess())
{
	if(!queryResult.allRows().isEmpty())
		for (N1qlQueryRow n1qlQueryRow : queryResult)
			if (n1qlQueryRow != null)
				System.out.println(n1qlQueryRow.toString());
}

The queryResult.toString() return "com.couchbase.client.java.query.DefaultN1qlQueryResult@6f1fba17"
and the n1qlQueryRow.toString() return nothing.

What did you mean in UPDATE statement and where is the mutationCount?

Thanks for your response.


#4

n1qlQueryRow.value() will return the result object


#5

Thanks hubo3085632,

It’s a UPDATE query and the queryResult.allRows().isEmpty() = true

I can’t get anything from the n1qlQueryRow.

If this is a SELECT query then I can get the json result from n1qlQueryRow.

But my question is to get the updated rows and I want to know how to check this if the document doesn’t exist.


#6
String query = "UPDATE testBucket SET available = 'true' WHERE type = 'blue'";
N1qlQuery qr = N1qlQuery.simple(query);
N1qlQueryResult queryResult = bucket.query(qr);
if(queryResult.parseSuccess())
{
//the data row in queryResult is null(no n1qlQueryRow in it)
}
  1. How do I know how many records have been updated?
  2. And if the document doesn’t exist, how to detect the situation?

My solution is to select the data first before updating it to make sure the document for searching exist.
The solution need twice query for each update query and may be wasting time.

But I still don’t know how many records have been updated.


#7

I think the mutationCount geraldss means, is queryResult.info().mutationCount()


#8

Thanks!!!
That’s what I want to know!