Suppose i have:
"companies": [
{
"companyName": "Test1506",
"companyId": "test1506_090bee58-c518-4791-b8c8-5de7eefec10b",
"role": "PARTNER"
},
{
"companyId": "somecompanyname_34f64628-a8f4-4e2d-969f-da45e8a04966",
"companyName": "somecompanyname",
"role": "DRIVER"
},
{
"companyName": "somnium",
"companyId": "somnium_6a8b779f-824d-41bd-83e6-bf1d146fa1e9",
"role": "ADMIN"
}
],
and i want change any field in one of object?
now i need complete stages:
- Get document and select “companies” fileld
- Filter needed companies in two variables “updateInCompany”, “restCompanies”
- Then merge in result companies:
q.Union(
q.Var('restCompanies'),
[q.Merge(q.Select([0], q.Var('updateInCompany')), { role: "ADMIN" })],
)
- Then update document with new “companies” field
The question is are exists another more pretty way to update field in object which in array?
Or which way is the best practice in such a case?
May be i can use index like this? (this not works):
{
name: 'user_companies_by_id',
source: q.Collection('users'),
serialized: true,
terms: [
{ field: ['ref'] },
{ field: ['data', 'companies', 'companyId'] },
],
}