How to partially update a document

Many times, it is very convenient to only update one attribute instead of loading everything into memory and then pushing everything onto the wire, specially when a few changed (let say only 1% had changed).

I did not find any element, except an issue opened from 2012/06 (http://www.couchbase.com/issues/browse/MB-5502), on how to partially update an existing document.

It seems like Couchbase only allow full update.

I was expecting that in N1QL one could do some commands such as:

  • DELETE
  • UPDATE (in particular this one)
  • INSERT

But it seems not (event if Keywords are reserved) and that N1QL is only focusing on SELECT (and yes some others like INDEX creation or droping).
In Java JDK 2.0, I can see the following :

  • get (equivalent to SELECT with KEYS keyword for 1 item)
  • query (where N1QL takes place)
  • insert (equivalent to INSERT)
  • remove (equivalent to DELETE except there is no “WHERE” condition)
  • replace and upsert : those ones are not clear
    What are the differences ? first one making an update only if it exists before, second making insert or replace (therefore not taking into account really its existence) ?
    Moreover is there one enabling the “partial” update ? or are they both as “full” update ?

If this is not yet available, when do you think it will be?

Thanks,
Frederic

Hi Frederic,

N1QL DP4 and beyond will include the following statements:

  • SELECT
  • INSERT
  • UPSERT
  • DELETE
  • UPDATE
  • MERGE

These will be analogous to their SQL equivalents, plus additional JSON / document capabilities (e.g. arrays and nested objects).

DP4 is still a preview version. It will be available in a couple of months.

Thanks.

OK, I understand. The dev is on going :wink:

Thanks

Hi Geraldss,

Delete and Update works fine from Admin Query portal. However, it is not happening when I try to execute the same delete/update statement from Spark. Is there a way how to use N1QL to delete/update from Spark?

Below is the code snippet I used. It runs fine but does not actually delete the document.

package testpackage
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.sql.SQLContext
import com.couchbase.client.java.query.N1qlQuery
import com.couchbase.spark.toSparkContextFunctions

object delete {
def main(args: Array[String]) {
val conf = new SparkConf().setAppName(“test”).setMaster(“local”).set(“com.couchbase.bucket.mybucket”, “”)

val sc = new SparkContext(conf)
val sqlContext: SQLContext = new SQLContext(sc)
try {
  deletedoc()(sc, sqlContext)

} catch {
  case e: Exception =>
    e.printStackTrace()
    sc.stop()
    System.exit(1)
} finally {
  sc.stop()
}

}

def deletedoc()(sc: SparkContext, sqlContext: SQLContext) = {
val query = "delete from mybucket where meta().id=‘birairports::France’"
sc.couchbaseQuery(N1qlQuery.simple(query))

}

}