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

QueryBuilder is replacing "id" by "_id" when filtering on a hash field #2550

Open
thomas-negrault opened this issue Sep 21, 2023 · 1 comment

Comments

@thomas-negrault
Copy link

thomas-negrault commented Sep 21, 2023

Q A
Version 2.5.2

Support Question

I am using this lib with DoctrineMongoDBBundle.

I store Stripe subscription into my User document. Users has a subscription embed document which has a stripeSubscription hash, which is the json provided by Stripe

Here is my mapping:

#[MongoDB\Document(collection: 'users', repositoryClass: UserRepository::class)]
class User
{
    #[MongoDB\EmbedOne(targetDocument: Subscription::class)]
    protected Subscription $subscription;
#[MongoDB\EmbeddedDocument]
class Subscription
{
    #[MongoDB\Field(type: Type::HASH, nullable: true)]
    protected null|array $stripeSubscription;

Here is a sample of my User document:

{
    _id: '1e4dc99cccd03b0898029756790625a4',
   subscription: {
        stripeSubscription: {
            id: 'sub_123',
            object: 'subscription',
            ...
        }
   }
}

I want to fetch users by subscription.stripeSubscription.id so I did:

        $subscriptionId="sub_123";
        $queryBuilder = $this->createQueryBuilder();
        $queryBuilder->field("subscription.stripeSubscription.id")->equals($subscriptionId);

        return $queryBuilder
            ->getQuery()
            ->getSingleResult();

But in the profiler, the formatted query is:

{
    "find": "users",
    "filter": {
        "subscription.stripeSubscription._id": "sub_123"
    },
    "limit": {
        "$numberInt": "1"
    },
    "$db": "myDb",
    "lsid": {
        "id": {
            "$binary": {
                "base64": "HWzvsCWeTlad1CBKpMSAuw==",
                "subType": "04"
            }
        }
    }
}

So my ->field("subscription.stripeSubscription.id") is converted to subscription.stripeSubscription._id with _id instead of id and it does not find my user.

Am I missing something here ? Is there an option to filter with a field containing .id without rewriting to ._id ?

Edit: I found a workaround, it's not perfect as it does 2 queries (one to find it and the other one to return an hydrated Document) but it does work:

$dbName = $this->getDocumentManager()->getConfiguration()->getDefaultDB();
$mongoClient = $this->getDocumentManager()->getClient();
$collection = $mongoClient->selectDatabase($dbName)->selectCollection('users');
$userArray = $collection->findOne(['subscription.stripeSubscription.id' => $subscriptionId]);

$user = $this->find($userArray['_id']);
@malarzm
Copy link
Member

malarzm commented Sep 27, 2023

I believe this resembles #2531 (comment)

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

No branches or pull requests

2 participants