Subdoc on non existing document?

Hi!

I am trying to figure out is there a way to auto create document if it is not exists yet and update some of it’s properties? Or should I upsert first and then mutate?

Thnks!

ps
Nodejs sdk 2.5 and Server 5.0.1

Hey @Cyb,

If you would be able to explain the exact semantics you’re looking for a little bit better, I can probably give you more specific guidance. However, the insert operation or the mutateIn operation coupled with createDoc (and createPath) are probably what you are looking for.

Cheers, Brett

Hi, @brett19

I need to mutate some doc fields but this doc may not exists yet. For now I am using basic scenario:

  1. mutate fields (if doc exists then everything ok)
  2. if mutate failed then insert new doc with those fields

Hoped that there is a better way to do it in 1 round trip…

ps:
here Sub-doc API - upsert auto-create document? some mention of make_doc flag but I could not find it in nodejs docs or sources :slight_smile:

Hey @Cyb,

You should be able to do this using something like the following:

bucket.mutateIn('mydoc', {upsert: true})
  .upsert('foo.bar', 10, {createParents: true})
  .upsert('foo.baz', 20, {createParents: true})
  .execute((err) => {
    // Completed!
  });

P.S. I’ve opened an issue related to the missing documentation for the upsert and insert options that the Node.js understands for a mutateIn operation:
https://github.com/couchbase/couchnode/blob/master/lib/bucket.js#L3613-L3618)
https://issues.couchbase.com/browse/JSCBC-529

Cheers, Brett

Yes, that’s what I am looking for :slight_smile:

@brett19, thanks a lot!

@brett19 can you explain difference of upsert and insert options for a mutateIn operation?

bucket.mutateIn('mydoc', {upsert: true})
  .upsert('foo.bar', 10, {createParents: true})
  .upsert('foo.baz', 20, {createParents: true})
  .execute((err) => {
    // Completed!
  });
bucket.mutateIn('mydoc', {insert: true})
  .upsert('foo.bar', 10, {createParents: true})
  .upsert('foo.baz', 20, {createParents: true})
  .execute((err) => {
    // Completed!
  });
bucket.mutateIn('mydoc', {upsert: true,insert: true})
  .upsert('foo.bar', 10, {createParents: true})
  .upsert('foo.baz', 20, {createParents: true})
  .execute((err) => {
    // Completed!
  });

Hey @socketman2016,

The difference is in the behaviour with the underlying document. Upsert semantics mean that if the underlying document does not exist, a blank document will be created and then the subdocument operations performed against it. Insert semantics mean that the operations will only execute if the document does not exist. Replace semantics (the default) mean that the operations only execute if the document exists already.

Cheers, Brett

Why we must use mutateIn with insert option?

What is the difference of the following codes

bucket.mutateIn('mydoc', {insert: true})
  .upsert('foo.bar', 10, {createParents: true})
  .upsert('foo.baz', 20, {createParents: true})
  .execute((err) => {
    // Completed!
  });
bucket.insert('mydoc', {
 foo:{
    bar:10,
    baz:20,
}
},(err) => {
    // Completed!
  });

Might wanna add it also to the new sdk documentation, cause it has been lost.
Also the correct value is not anymore upsert, but “upsertDocument”.
By the way, can we please go back to the old documentation system? The new one is a real nightmare of readabilty and usability.

Hi @00christian00 thanks for the feedback. @Richard_Smedley can you please assist

Added a ticket to include this in our SDK docs too:
https://issues.couchbase.com/browse/DOC-9236

Thanks for the feedback!

1 Like

By the way, can we please go back to the old documentation system? The new one is a real nightmare of readabilty and usability.

Hi @00christian00 just wanted to get a clear understanding on the feedback for the documentation, did you mean the SDK documentation or the API documentation?

SDK: Couchbase Node.js SDK 4.2 | Couchbase Docs
API: couchbase

We’ll definitely take this feedback on board, just wanted to know which area to take note of!

1 Like

Hi Marie,
I’m talking about the Api reference. The previous one had a clear differentiation between classes, events ,etc.
Had a quick menu on the top to switch between classes rapidly.
Have some navigation issues, for example if I am in this page:
https://docs.couchbase.com/sdk-api/couchbase-node-client/index.html

I should be in the top page, I should be able to see all the main classes at least, but all I can see is export and connect.
The readability of the text is also much worse than before. Before we had a clear distinction between parameters and methods and we had alternating colors for the parameter table, now it’s almost all black and white.

Overall I am finding it much harder to use the new documentation system, so much that most of the time it is easier to browse the source code or the sdk examples( when available).

Regards
Chris