#Storing relationships as strings instead of ObjectIds

8 messages · Page 1 of 1 (latest)

velvet marlin
#

I use Payload CMS to update records in MongoDB. I noticed that when I use relationships to link different collections they are stored not as ObjectIds but as strings.
The code
{
name: 'occupationIds',
type: 'array',
label: 'Occupation',
required: true,
fields: [
{
name: 'occupationId',
type: 'relationship',
relationTo: 'occupations',
required: true,
},
],
}

results in a record

occupationIds: Array (2)
0: Object
occupationId: "66b280672a4e4285b87eb500"
id: "673b3b0d118c4d2064ef5cbb"
1: Object
occupationId: "66b280662a4e4285b87eb1d1"
id: "673b3b0d118c4d2064ef5cbc

My concern is that storing strings instead of an ObjectId will cause worse performance and slower querying. Can anything be done with it? Or there're no reasons to worry?

foggy kayakBOT
hot estuary
#

when fetching the data you need to adjust the depth

velvet marlin
# hot estuary https://payloadcms.com/docs/queries/depth

thanks, a concept of depth is something I didn't know about. At the same time I don't understand how to make it work

{
name: 'occupationIds',
type: 'array',
label: 'Occupation',
required: true,
fields: [
{
name: 'occupationId',
type: 'relationship',
relationTo: 'occupations',
required: true,
** maxDepth: 0, **
},
],
},

still results in

occupationIds: Array (2)
0: Object
occupationId: "66b280672a4e4285b87eb500"
id: "673b3b0d118c4d2064ef5cbb"

hot estuary
#

By setting a max depth of 0 on the field you are basically saying that you only want to store the ids

#

Remove the maxDepth

#

And when you fetch the data using rest or the local api pass in the depth as 1 or 2