Hello! Is it possible have an array of IDs refere...
# community-help
d
Hello! Is it possible have an array of IDs reference another collection? For instance, our contacts can have multiple tags. Would it be possible to reference the tags collection using an array of
tag_ids
on the contact, or would I need to build another collection like
contact_tags
for instance that has a reference to
contacts
and a reference to
tags
? I was able to create the collection with this field and fill the data, but I'm having trouble getting the queries to work. I'm guessing my syntax might be wrong or maybe it's not possible?
Copy code
let searchParams = {
      q: '*',
      query_by: '',
      filter_by: '$tags(id:*)',
      include_fields: '$tags(*, strategy: nest_array)',
      page: 1,
      per_page: 50,
      // sort_by: '$tags(name:asc)',
    };
h
Hey @Dakota Sorenson are you getting the error when using
sort_by
?
d
@Harpreet Sangar Yeah I get the error when I try to reference the tags collection on sort by, filter by, etc
h
I think it should only return error for
sort_by
.
The reason is that if a document has multiple references, we cannot decide which value of the referenced field to pick for sorting.
d
yeah that makes sense! I was mostly trying to get the include fields to work to return all the tags w each document
do I need both the filter and include fields for that to work? with both it throws the error, with just the include fields it returns results but doesn't actually include the tags
h
Since you are querying the collection that has the reference field, you don't have to specify filter_by.
include_fields: $tags(*)
should be enough.
d
I believe I had tried it with just
include_fields: '$tags(*, strategy: nest_array)'
as well and the query worked but didn't include the tags
in the documents
h
Could you share your schema of
contacts
collection? Especially the definition of
contact_card
d
@Harpreet Sangar Sorry for the late reply! Here's the full schema. I ended up removing the reference for now since I couldn't get it working, and I added
tag_names
in the meantime.
tag_ids
is the field I tried to add the reference to (I commented it out in the schema)
Copy code
let contactsSchema: CollectionCreateSchema = {
    name: collectionName,
    enable_nested_fields: true,
    fields: [
      { name: 'account_id', type: 'string', optional: true, sort: false },
      { name: 'authorized_account_ids', type: 'string[]', optional: true, sort: false },
      { name: 'added_at', type: 'int64', optional: true, sort: true },
      {
        name: 'contact_card.associated_card_id',
        type: 'string',
        optional: true,
        sort: false,
      },
      {
        name: 'contact_card.associated_card_name',
        type: 'string',
        optional: true,
        sort: true,
      },
      { name: 'contact_card.company', type: 'string', optional: true, sort: true },
      {
        name: 'contact_card.contact_card_id',
        type: 'string',
        optional: true,
        sort: false,
      },
      {
        name: 'contact_card.contact_id',
        type: 'string',
        optional: true,
        sort: false,
      },
      {
        name: 'contact_card.family_name',
        type: 'string',
        optional: true,
        sort: true,
      },
      {
        name: 'contact_card.given_name',
        type: 'string',
        optional: true,
        sort: true,
      },
      { name: 'contact_card.is_live_contact', type: 'bool', optional: true, sort: false },
      { name: 'contact_card.source', type: 'int32', optional: true, sort: false },
      { name: 'contact_card.tag_ids', type: 'string[]', optional: true, sort: false // reference: 'tags.id' },
      { name: 'contact_card.tag_names', type: 'string[]', optional: true, sort: false },
      { name: 'contact_card.title', type: 'string', optional: true, sort: true },
    ],
    token_separators: ['+', '-', '@', '.'],
  };
Also, if I'm reading the documentation correctly there's no support for searching by a string field in another collection yet, right? Or is there a way to do that?
h
Can you share a sample document also? It seems like
contact_card
is an object in your document. If so, you can define
Copy code
{ name: 'contact_card', type: 'object'}
Then the join should work.
👀 1
d
Yes
contact_card
is an object in the doc! I can share a sample shortly Is it best practice to define an object like that normally on the schema, or is it just needed for the joins? I have other schemas where I'm using an object in the doc so I'm wondering if I should update those to add the specific object type/definition as well?
Copy code
{
  account_id: 'wzWAfweiojjfowij6Zgu9tVFuH3',
  added_at: 1694119056432,
  authorized_account_ids: [ 'wzWAfweiojjfowij6Zgu9tVFuH3' ],
  contact_card: {
    associated_card_id: 'JmSNJDOWIJOnlAcxao7L8',
    associated_card_name: 'Work',
    company: 'Fake Company Name',
    contact_card_id: 'WRjMlAQFJIOWQJSpHBNDs',
    contact_id: 'MR06iOxJ9WOKFPQwuSLm03',
    given_name: 'Person',
    family_name: 'Name',
    source: 2,
    tag_ids: [ 'fwoeufOUFEOWU' ],
    tag_names: [ 'tag' ],
    title: 'Person Title'
  },
  id: 'eagWJDOFIOQOdO9lLS0'
}
h
Is it best practice to define an object like that normally on the schema, or is it just needed for the joins?
I've only tested the reference field inside an object where I declared the object field. I'm not sure why joins didn't work without it. I'll check this.
🙏🏻 1
d
Awesome thank you! I can start adding that if it's needed, I luckily haven't run into any issues without having it so far (other than the joins)