# API data query filtering

**URL:** <https://forum.builder.io/t/api-data-query-filtering/1959>\
**Category:** Technical Questions\
**Tags:** react, api, nextjs\
**Created:** [March 26, 2022, 8:51pm UTC](https://forum.builder.io/t/api-data-query-filtering/1959 "2022-03-26T20:51:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![radikris](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@radikris](https://forum.builder.io/u/radikris)\
**Post date:** [March 26, 2022, 8:51pm UTC](https://forum.builder.io/t/api-data-query-filtering/1959/1 "2022-03-26T20:51:40Z")

</div>

Hey!

I would like to write a query for for my endpoint with these conditions:  
My data is localized:  
[https://cdn.builder.io/api/v2/content/help-data?apiKey=32f148657e2646be8562eb4e6ebfa190](https://cdn.builder.io/api/v2/content/help-data?apiKey=32f148657e2646be8562eb4e6ebfa190)

Here is an example result for it:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/f/fc6bd80cf211350734342bef77adb9fb85751ae3.png)

_ **Example: query string= ‘question’, locale= 'en’** _  
My conditions:  
I would like to search in my specific locale  
AND  
Search in the title OR the description property (case insensitive and search or description includes my query string)

I tried something, but it is clearly not working:

```auto
const searchResult = await builder.getAll('help-data', {
      options: {
        noTargeting: true,
        query: {
          'data.helplist.[locale].title.$options=i.$regex': searchQuery,
        },
      },
    })

```

How can I pass my locale to the query string, and meet all my conditions above, please help me, I am not very used with this complex mongo syntax.

Thanks in advanced!

---

<div class="post-metadata">

**Author:** ![ancheetah](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/ancheetah/32/751_2.png) [@ancheetah](https://forum.builder.io/u/ancheetah)\
**Post date:** [March 28, 2022, 7:01pm UTC](https://forum.builder.io/t/api-data-query-filtering/1959/2 "2022-03-28T19:01:35Z")

</div>

Hey @radikris! Since you’re looking for a regex inside of an array you’ll want to use the `$regex` and [`$elemMatch`](https://www.mongodb.com/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch) operators. Then your query would look like this to search for something in the title:

```auto
query: {
          'data.helplist.[locale].$elemMatch': { title: { $regex: searchQuery } }
        }

```

---

<div class="post-metadata">

**Author:** ![radikris](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@radikris](https://forum.builder.io/u/radikris)\
**Post date:** [March 28, 2022, 7:52pm UTC](https://forum.builder.io/t/api-data-query-filtering/1959/3 "2022-03-28T19:52:47Z")

</div>

Thanks, thats one more step closer to the solution:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/e/e80db0fcdd3675e88051e5143f4cd755d6dcb7c5.png)

1. How can I pass the locale dynamically to this query, because this list is localized with builder, but I only want to search in the list[locale]. (see my example above)

2. How can I search for ‘title’ OR ‘description’?

---

<div class="post-metadata">

**Author:** ![ancheetah](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/ancheetah/32/751_2.png) [@ancheetah](https://forum.builder.io/u/ancheetah)\
**Post date:** [March 30, 2022, 5:10pm UTC](https://forum.builder.io/t/api-data-query-filtering/1959/4 "2022-03-30T17:10:24Z")

</div>

1. You’re on the right track! Javascript does not allow template strings for property names. You can wrap it in [brackets](https://codepen.io/ancheetah/pen/LYeLqOz) instead to make it a computed property.  
`[`data.helplist.${locale}.$elemMatch`]`

2. You can use the `$or` operator in the value:

```auto
{
   $or: [
      { description: { $regex: searchQuery } },
      { title: { $regex: searchQuery } }
   ]
}

```

---

<div class="post-metadata">

**Author:** ![radikris](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@radikris](https://forum.builder.io/u/radikris)\
**Post date:** [April 19, 2022, 8:28am UTC](https://forum.builder.io/t/api-data-query-filtering/1959/5 "2022-04-19T08:28:41Z")

</div>

Hi @ancheetah !  
Everything works now like a charm, except the case insensitive part:

Here is what I have:

```auto
 const searchResult = await builder.getAll('help-data', {
      options: {
        noTargeting: true,
        query: {
          [`data.helplist.${locale}.$elemMatch`]: {
            $or: [
              { description: { $regex: searchQuery, $options: 'i' } },
              { title: { $regex: searchQuery, $options: 'i' } },
            ],
          },
        },
      },
    })

```

I followed the official mongodb docs:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/6/61658630bd28c73d5f7256dd2c834966b76b31e2.png)

But let me show you an example:  
Search for ‘masodik’ or ‘Masodik’ are not the same:  
“masodik”:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/1/11649cae7a9c48549d567f62664dff072d015068.png)

“Masodik”:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/a/a5e4fabe798687a91e88348e380c4d34f0fa5321.png)

I don’t know what could cause the issue, can you please check this out?  
Thanks

---

<div class="post-metadata">

**Author:** ![TimG](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/timg/32/714_2.png) [@TimG](https://forum.builder.io/u/TimG)\
**Post date:** [April 20, 2022, 1:51am UTC](https://forum.builder.io/t/api-data-query-filtering/1959/6 "2022-04-20T01:51:41Z")

</div>

@radikris I wonder if the `[`data.helplist.${locale}.$elemMatch`]` isnt being calculated to what you expect? I would console log to make sure that is what you want it to be

I was able to get results correctly in the [Builder.io: Drag and drop page builder and CMS](http://builder.io/api-explorer) with the query object set to:

```auto
{
    data: {
        helplist: {
            en: {
                $elemMatch: {
                    $or: [{
                            description: {
                                $regex: 'vegyes',
                                $options: 'i'
                            }
                        },
                        {
                            title: {
                                $regex: 'vegyes',
                                $options: 'i'
                            }
                        }
                    ]
                }
            }
        }
    }
}

```

or

```auto
{"data":{"helplist":{"en":{"$elemMatch":{"$or":[{"description":{"$regex":"vegyes","$options":"i"}},{"title":{"$regex":"vegyes","$options":"i"}}]}}}}}

```

And it was giving what appeared to be case-insensitive results, check it out yourself and see if that helps you troubleshoot more

---

<div class="post-metadata">

**Author:** ![radikris](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@radikris](https://forum.builder.io/u/radikris)\
**Post date:** [April 20, 2022, 6:12am UTC](https://forum.builder.io/t/api-data-query-filtering/1959/7 "2022-04-20T06:12:19Z")

</div>

okey, thanks for pointing this out for me, I realized it was not the issue with my querying, I had an extra filtering local, where I did not add the toLowerCase() 🤦‍♂️  
Thank you, sorry for this unneccessary question 😃 but hope someone else can learn from it
