Converting documents using N1QL

Hi there, my question is a little urgent. I have data like this in my couchbase:

{
“mappings”: {
“/”: “Ana sayfa”
},
“platform”: “WEB”
}
I want to convert all data like this:
{
“/”: {“viewLabel”:“Ana Sayfa”}
“platform”: “WEB”
}

So I want to share old version:

{
“_class”: “com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity”,
“id”: “whitelabel::WEB::screenNamesMapping”,
“mappings”: {
“/”: “Ana sayfa”,
}
}

I want to create new document with above id ( “id”: “whitelabel::WEB::screenNamesMapping”,
) and delete the old one.

I want to create and convert like this:

{
“_class”: “com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity”,
“id”: “whitelabel::WEB::screenNamesMapping”,
“mappings”: {
“/”: { “viewLabel”: “Ana sayfa” } ,
}

Help please. I need to write script. I want to create new document with related id, then delete the old one, it could be multipe N1QL

I should not update the old data, new data should have new key, and I should edit the new key with the old one and delete the old one. I need to do with that way.

Not sure why you can’t do update. How do u differentiate old and new document so that you can delete old documents.

You can use INSERT INTO SELECT and then DELETE .

INSERT INTO default VALUES("k0001", { "_class": "com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity", "id": "whitelabel::WEB::screenNamesMapping", "mappings": { "/": "Ana sayfa" } });

INSERT INTO default (KEY t.id, value t)
    SELECT OBJECT_PUT(t,"mappings",OBJECT_PUT(t.mappings,"/",{"viewLabel":t.mappings.`/`})) AS t
      FROM default AS t
      WHERE  _class ="com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity";

DELETE FROM default AS d
WHERE d._class ="com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity"
      AND d.mappings.`/`.viewLabel IS MISSING;

https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/insert.html

1 Like

Thanks for the rypel vsr1. Just one more question, I don’t want to do it manually, I have many data on my couchbase like this:

{
“mappings”: {
“/”: “Ana sayfa”
},
“platform”: “WEB”
}

the values of “/” and :“Ana sayfa” and also platform changes, it should do automatically :confused: Sorry I am new, but I 'll do try to understand but a little help would awesome for me.

Ok, I can update because I asked to my leader’s opinion.

“/” must be constant and value can be vary. If not give full example.
If mappings has field “/”: val it coverts into “/”: { “viewLabel”: val } irrespective of platform for all documents.
Do you want convert multiple fields of mappigns or all mappings this new format? If yes give a example.

1 Like

Old:

{
“_class”: “com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity”,
“id”: “whitelabel::WEB::screenNamesMapping”,
“mappings”: {
“/”: “Ana sayfa”,
“/career/”: “Kariyer”,
“/company/”: “Şirket Hakkında”,
“/products/”: “Ürünler”
}
}

New

“mappings”: {
“/”: { “viewLabel”: “Ana sayfa” } ,
“/career/”: { “viewLabel”:“Kariyer”},
“/company/”: { “viewLabel”:“Şirket Hakkında”},
“/products/”: { “viewLabel”:“Ürünler”}
}

So I said I can not update but well I can now. I am trying this now updating old document. Like above example I can have multiple key and its values, so I want do it dynamically. I need to get key, add viewLabel attribute to its value and keep going. It is hard to understand for me to write N1QL

N1QL works same as SQL, Data representation is in objects and it has additional data types arrays.

INSERT INTO default VALUES("k0001", { "_class": "com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity", "id": "whitelabel::WEB::screenNamesMapping", "mappings": { "/": "Ana sayfa", "/career/": "Kariyer", "/company/": "Şirket Hakkında", "/products/": "Ürünler" } });

UPDATE default AS d SET
       d.mappings = OBJECT v.name: {"viewLabel":v.val}  FOR v IN OBJECT_PAIRS(d.mappings) END
WHERE d._class ="com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity"
      AND d.mappings IS NOT MISSING;
1 Like