#Query select single array item

4 messages · Page 1 of 1 (latest)

warm quarry
#

If i have an array on a collection, how do you select a specific array item only in a query

i can find a parent document based on an arrays sub field fine with the where clause, but how do i query just the array value. I always seem to get the full array back.

eg i have an array of sections

{
    type: 'array',
    name: 'sections',
    fields: [
      {
        type: 'row',
        fields: [
          {
            name: 'title',
            type: 'text',
            admin: {
              width: '50%',
            },
          },
          {
            name: 'valuea',
            type: 'text',
            admin: {
              width: '50%',
            },
          },
        ],
      },
    
      ........other fields.......      

    ],
    admin: {
      initCollapsed: true,
      components: {
        RowLabel: rowLabel({ template: '{{title}}' }),
      },
    },
  },

I always get a full array of the sections returned

So i can do this and restrict only the title and valuea fields to come back in the array:

select: {
    sections: {
      title: true,
      valuea: true,
    }
  },

And i can do this to only match parent documents where the section.valuea matches, but it still returns the whole sections array

where: {
  'sections.valuea': {
    equals: 'myvalue',
  },
},

Is there a way of only including section array elements based on a condition?
I was hoping for something like this to narrow down the returned selections array but it doesnt make any difference:

select: {
  sections: {
    title: true,
    valuea: true,
    where: {
      valuea: {
        equals: 'myvalue',
      },
    },
  },
},
pearl masonBOT
opaque mulch
#

I don't believe you can select a single array member using the API's. You would have to do that in your own code after fetching the array as of now

#

The issue is you would have to pass some kind of index to know which array member to query