#Querying Accommodations with Multiple Amenities Criteria

32 messages ยท Page 1 of 1 (latest)

fading prairieBOT
#

Hey guys,
I'm going crazy because of a simple issue.
I've got a collection of accommodations and a collection of amenities.
The accommodations have a relationship field to amenities with hasMany: true.
I'm using the LocalAPI.

I'm just trying to query accommodations that have every selected amenities, and not one of them. I've tried many types of query, but I still don't have the excepted result.

For example:

  • first acoommodation has: wifi, king size bed and private spa
  • second accommodation has: wifi, king size bed and TV
  • third accommodation has: wifi, TV and air conditionning
    I want to query accommodations with wifi AND king size bed, so the first and second accommodations

Can anyone explain how to query relationship fields to achieve this type of results ?
Thanks ๐Ÿ‘

wheat mauveBOT
#

Original message from @chrome kindle - Moved from #general message

restive bridge
#

Hey @chrome kindle so I am guessing here as I haven't seen any of the queries you tried, but in against a hasMany relationship means "the array contains at least one of these" โ€” which will give you all three accommodations

Same goes for { amenities: { equals: [a, b] } }

#

so you could try this:

const result = await payload.find({
  collection: 'accommodations',
  where: {
    and: [
      { amenities: { equals: wifiId } },
      { amenities: { equals: kingSizeBedId } },
    ],
  },
})
#

are you querying by string or by slug?

chrome kindle
chrome kindle
#

and id as number and string

#

of course, while using slug, the key was "amenities.slug"

restive bridge
#

can you share some of the queries you tried?

chrome kindle
#

that's the query

#

i will share some where queries coming from the buildWhereQueries call

restive bridge
#

yes I would like to see what's going on there

chrome kindle
#

here i should have 1 result:
{ and: [ { 'amenities.slug': { equals: 'chauffage' } }, { 'amenities.slug': { equals: 'cheminee' } } ] }

#

same amenities but with ids:

{
  and: [ { amenities: { equals: '6' } }, { amenities: { equals: '5' } } ]
}
restive bridge
#

I would toss some logging in like this before the find call and see what comes back

const where = buildWhereFilters(filters)
console.log(JSON.stringify(where, null, 2))
const filteredResult = await payload.find({ collection: 'accommodations', where, ... })
chrome kindle
#

taht's what i'm doing ^^

#

all of these queries have the same result

#

0 reslut instead of 1 in this case

restive bridge
#

are you using Postgres?

chrome kindle
#

yes

#

i dndn't mention it sorry

restive bridge
#

no worries, I had a suspicion

chrome kindle
#

Binaries:
  Node: 24.13.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: 10.19.0
Relevant Packages:
  payload: 3.85.0
  next: 16.2.6
  @payloadcms/db-postgres: 3.85.0
  @payloadcms/drizzle: 3.85.0
  @payloadcms/graphql: 3.85.0
  @payloadcms/next/utilities: 3.85.0
  @payloadcms/plugin-cloud-storage: 3.85.0
  @payloadcms/plugin-seo: 3.85.0
  @payloadcms/richtext-lexical: 3.85.0
  @payloadcms/storage-azure: 3.85.0
  @payloadcms/translations: 3.85.0
  @payloadcms/ui/shared: 3.85.0
  react: 19.2.6
  react-dom: 19.2.6
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Thu Jun  5 18:30:46 UTC 2025
  Available memory (MB): 31935
  Available CPU cores: 32
#

Thanks for helping me btw ๐Ÿ‘

restive bridge
#

Of course!

#

I think this might be a limitation of the drizzle adapter, so I'm goig to consult some experts on our team

chrome kindle
#

Ok, thanks a lot ๐Ÿ‘Œ