Subdoc on non existing document?



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?


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…

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('', 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:

Cheers, Brett


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

@brett19, thanks a lot!