Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$elemMatch operator does not work in the new SDK #3007

Closed
kaceycleveland opened this issue Feb 12, 2024 · 6 comments · Fixed by #3250
Closed

$elemMatch operator does not work in the new SDK #3007

kaceycleveland opened this issue Feb 12, 2024 · 6 comments · Fixed by #3250
Assignees

Comments

@kaceycleveland
Copy link

Describe the bug
A clear and concise description of what the bug is.

We are in the process of porting over to the new SDK (which I saw was marked as 1.0.0, nice!)
Unfortunately, the old functionality of the API could not be replicated with the new API with $elemMatch.

To Reproduce
Not a concrete reproduction but my test script is as follows:

// @ts-nocheck ignore all issues as this is a script
import { builder } from '@builder.io/sdk';
import { fetchEntries } from '@builder.io/sdk-react';

builder.init(process.env.NX_BUILDER_IO_PUBLIC_KEY);

const callBuilder = async () => {
  const results = await builder.getAll('blog-article', {
    options: {
      sort: {
        createdDate: -1,
      },
      enrich: true,
    },
    query: {
      'data.categories': {
        $elemMatch: {
          'category.id': {
            $in: JSON.stringify(['d491820ec5da422199336a73f388f354']).trim(),
          },
        },
      },
    },
    limit: 5,
    omit: 'data.blocks',
  });
  const resultsNew = await fetchEntries({
    model: 'blog-article',
    apiKey: process.env.NX_BUILDER_IO_PUBLIC_KEY!,
    options: {
      sort: {
        createdDate: -1,
      },
      enrich: true,
    },
    query: {
      'data.categories': {
        $elemMatch: {
          'category.id': {
            $in: JSON.stringify(['d491820ec5da422199336a73f388f354']).trim(),
          },
        },
      },
    },
    limit: 5,
    omit: 'data.blocks',
  });

  const pages = results.map((res) => res.data.title);
  pages.forEach((article) => console.log(article));

  if (results.length && results[0]) {
    console.log(Object.keys(results[0].data));
    console.log(results[0].data.categories);
  }

  console.log('NEW -------------');

  const pagesNew = resultsNew.map((res) => res.data.title);
  pagesNew.forEach((article) => console.log(article));

  if (resultsNew.length && resultsNew[0]) {
    console.log(Object.keys(resultsNew[0].data));
    console.log(resultsNew[0].data.categories);
  }
};

callBuilder();

Expected behavior
A clear and concise description of what you expected to happen.
The return in the repo test script should be the same results but the new API returns no results.
It would be helpful to have a debug option attached to the builder API client to output some additional information such as the query keys being passed to the request or something.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

@kaceycleveland
Copy link
Author

Posting for context,
https://forum.builder.io/t/new-sdk-returning-different-then-the-old-sdk/4975/7

There is an example of getting it working with the new SDK via the options param and encoding URLSearchParams but its not great.

I really think this is a large issue with the new SDK as it blocks a large portion of querying.

@kaceycleveland
Copy link
Author

Update from the above post, the work around and the example query provided in the above thread do not work.

From my POV, the $elemMatch operator seems to be broken or at least I have yet to be able to find a query that returns the correct results.

Mentioned in the thread, but I really feel like this is an important functionality to cover especially when we were told we should be using the new SDK. I would take a stab at creating a PR to address this in the new SDK but I would first need to find out if its even a problem with the SDK itself or if its the new content API. So far, I have not been able to get it working properly with the API query itself so that would be the first step if someone could provide a working example.

@samijaber
Copy link
Contributor

@kaceycleveland Thanks for reporting this bug with great detail. We will let you know when we have an update on the root cause and/or have a workaround for you.

@chub7
Copy link

chub7 commented Feb 29, 2024

We encountered the same issue with the $elemMatch. Looking forward to a resolution.

@mikehuebner
Copy link

Hey y'all, any updates on this?

@samijaber samijaber self-assigned this Apr 9, 2024
samijaber pushed a commit that referenced this issue May 1, 2024
…t for mongodb (#3250)

## Description

This PR updates the query option to use `flattenMongoQuery` which is
basically flatten but when we get an `$key` we just return the object as
is as, so that it properly gets parsed in our API (mongo query).

**Jira**
https://builder-io.atlassian.net/browse/ENG-5268

**Loom**
https://www.loom.com/share/10493e4fd2a24b40937590f7f690bff2

Fixes #3007
@samijaber
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants