#community-help

Indexing Nested Fields in Arrays

TLDR Amrit was trying to index nested fields in arrays but didn't use a flat format. Kishore Nallan suggested using dot notation and updating the type to int32[].

Powered by Struct AI
11
4mo
Solved
Join the chat
May 17, 2023 (4 months ago)
Amrit
Photo of md5-d714a3e42b6c1a6326f383b0ecdaae8b
Amrit
09:09 AM
const fields: Array<CollectionFieldSchema> = [
      { name: 'productId', type: 'string' },
      { name: 'name', type: 'string' },
      {
        name: 'brand',
        type: 'object',
        facet: true,
        index: true,
        fields: [
          { name: 'name', type: 'string', facet: true },
          { name: 'description', type: 'string', index: false, optional: true },
          { name: 'logo', type: 'string', index: false, optional: true },
          { name: 'cover', type: 'string', index: false, optional: true },
        ],
      },
      {
        name: 'variants',
        type: 'object[]',
        facet: true,
        index: true,
        fields: [
          { name: 'combination', type: 'string[]', facet: true },
          { name: 'ssin', type: 'string' },
          { name: 'brandSku', type: 'string', facet: true },
          { name: 'sellingPrice', type: 'int32' },
          { name: 'finalListingPrice', type: 'int32' },
          { name: 'internalDiscountPercentage', type: 'int32' },
        ],
      },
    ];

this is not indexing nested fields
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
09:14 AM
This doesn't look right. The fields must be flat. But here, you have type: object[] for variants field and then inside it again have a list of fields.
09:15
Kishore Nallan
09:15 AM
I mean this:

{
        name: 'variants',
        type: 'object[]',
        facet: true,
        index: true,
        fields: [
          { name: 'combination', type: 'string[]', facet: true },
          { name: 'ssin', type: 'string' },
          { name: 'brandSku', type: 'string', facet: true },
          { name: 'sellingPrice', type: 'int32' },
          { name: 'finalListingPrice', type: 'int32' },
          { name: 'internalDiscountPercentage', type: 'int32' },
        ],
      },
09:17
Kishore Nallan
09:17 AM
To define properties of the combination field inside variants object, you should have this:

{"name": "variants.combination", "type": ...}
09:17
Kishore Nallan
09:17 AM
Use dot notation to refer to nested field names.
Amrit
Photo of md5-d714a3e42b6c1a6326f383b0ecdaae8b
Amrit
10:03 AM
from console
Image 1 for from console
10:03
Amrit
10:03 AM
Image 1 for
10:04
Amrit
10:04 AM
variants: [
        {
          combination: ['2 MONTH'],
          ssin: 'SOSHS1311',
          brandSku: 'PG-120-2 MONTHS',
          sellingPrice: 2300,
          finalListingPrice: 2300,
          internalDiscountPercentage: 0,
        },
        {
          combination: ['1 MONTH'],
          ssin: 'SOSHS1310',
          brandSku: 'PG-60-1 MONTHS',
          sellingPrice: 1200,
          finalListingPrice: 1200,
          internalDiscountPercentage: 0,
        },
data that i try to insert
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:14 AM
Type must be int32[]
10:14
Kishore Nallan
10:14 AM
Because it's a field inside an array of objects so must be an array
10:14
Kishore Nallan
10:14 AM
In rc builds we have better error message for this.