shays10
shays10

Reputation: 539

Couchbase - Creating new property in all document types Foo where its value is taken from document type Bar?

Foo: {
 id: 100,
 barId: 123, 
 startDate: "12/12/79", //new field to be added 
} ,    

Bar: { 
 id: 123,
 startDate: "12/12/79",
}

We need to create a new property 'startDate' in all document types 'Foo' that its value is taken from document type 'Bar'.

All 'Foo' documents contain a "foreign key" to Bar (a field called 'BarId').

What is the easiest way to do it besides retrieving and updating all entities programmatically ? (Couchbase version is 3.0)

Upvotes: 2

Views: 162

Answers (3)

shays10
shays10

Reputation: 539

UPDATE didn't quite work for me since I need to perform some sort of a "join" between 2 document types. But it looks like Couchbase's MERGE statement is just what I needed.

http://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/merge.html

We'll upgrade to 4.1 and see how it goes.

Upvotes: 1

Simon Baslé
Simon Baslé

Reputation: 28351

As TAM said, you could probably do that with UPDATE statement from N1QL in Couchbase 4.1... but you're using 3.0.

In Couchbase 3.x, your best best is to write:

  1. a view that will allow you to get IDs of all documents of type Foo
  2. a script that uses a SDK to:
    1. query that view
    2. retrieve each Foo doc
    3. get the Bar id from the content of the Foo doc
    4. fetch the Bar doc
    5. update the Foo content accordingly and save (update) the Foor document in db

Upvotes: 4

TAM
TAM

Reputation: 1741

I guess you mean some declarative solution resembling SQL UPDATE. Such a thing isn't available for CB 3, but since CB 4.1, N1QL has an UPDATE statement. Check http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/update.html.

Upvotes: 4

Related Questions