DOCUMENT REFACTORING
Suppose that we have some documents stored in a collection, it could
be a customers collection, and that we stored only one address for
each document item in the first version of the documents/records.
But... in a new version of these records we want to create an array
containing the addresses related to that person, so we can store more
than one address to each customer.
So we could use something like this:
UPDATE customers SET addresses[] = {"type": "home", "address":
customers.address, "city": customers.city}
DELETE address FROM customers
DELETE city FROM customers
or:
DELETE address, city FROM customers
The UPDATE command should create the 'addresses' property, set it as
an array, and add the created object to it.
Then the DELETE command can be used to remove the old properties from
each document/record.
----------------------------------------------------------------
I don't know if the 'customers.' is really needed in the query. The
statement below shouldn't work?
"address": address
Another question: how the engine may to interpret the expression below
inside of a UPDATE statement?
address: address
Should it treat what is at the left of the colon as a string or it may
to pass the field/property value as the key to the created property?
----------------------------------------------------------------
DOCUMENT VERSIONING
In situations like this, it is common to use a 'version' property in
each document to store the version number for the structure in use
[1]. Because sometimes we have a lot of documents in a collection and
they can have different structure format.
Then, to update the structure, we can use:
BEGIN
UPDATE customers SET addresses[] = {"type": "home", "address":
customers.address, "city": customers.city}, version=2 WHERE version=1
DELETE address, city FROM customers WHERE version=2
COMMIT
I think that it makes more sense to use these commands when we cannot
put them inside of a transaction...
[1]
http://www.infoq.com/presentations/Nokia-Lessons-Learnt-Migrating-into-a-Classic-NoSQL