How to write update/upsert/insert query in jsp?

I want to update “Amount[0]” in {
“AccountNumber”: [
“1000000001”,
“1000000002”
],
“Type”: [
“Savings”,
“Checking”
],
“Amount”: [
“3985”,
“4803”
]
}
that is I want to change “3985” to “abcd” using jsp n1ql.
How can I write a query for the same?
key is “1001” and bucket is “Accounts”

Hi @shreyagoel0911

You could use the Sub-Docuemnt API to replace the value. This is the most efficient way to manipulate discreet parts of your data as you only send the changes you want across the connection instead of the whole document. You can also use the Sub-Doc to retrieve parts of your document too.

An example to replace Amount[0] in your document could work like this:

DocumentFragment<Mutation> result = bucket
    .mutateIn("document-id")
    .replace("Amount[0]", "abcd")
    .withCas(1234L) // optional but best to use it incase someone else has changed the value
    .execute();

You can read more about the Sub-Doc API here.

Thanks

Thank you so much! Great help :smile:

Are there packages that I need to import as well? I keep getting an error in DocumentFragment result saying that DocumentFragment doesn’t take a parameter and in .mutateIn(“document-id”) that cannot find symbol mutateIn in Bucket.

No, I don’t believe you need any additonal packages.

@subhashni @daschl Can you add any more information regarding the error with the above sample? Thanks

Is there any package for Sub document API?

No, I don’t think so. It should all come with the main SDK. You will need some additional namespaces though.

Thank you! It worked. I figured out my error. I wasn’t using the latest java idk so all the functions weren’t available. Thank you once again!