mongodb / mongo-php-driver

The Official MongoDB PHP driver
https://pecl.php.net/package/mongodb
Apache License 2.0
890 stars 205 forks source link

migrated from mongo 6 to 8 and a query keeps running #1720

Open markitosgv opened 1 month ago

markitosgv commented 1 month ago

Bug Report

Environment

I have a cluster in mongo 6, testing I have migrated to mongo 8, with the 1.20 driver.

There is an aggregate query that when executed remains undefined, are the same indexes and all the same as the dataset of mongo 6.

However this query if I run it from a mongo client works fine, so I suspect that there is a problem with the driver or the php library.

Its something related to lookup query, because if I remove lookup it works ok. So maybe is something I'm missingn about new way to do lookup queries on mongo 8?

the query is this:

db.collection.aggregate([
  {
    $match: {
      "readers": { $ne: { $oid: "650825bcbf6db135710001fb" } },
      "company": { $oid: "6509cef01f97635bac0cc36c" },
      "deletedAt": null,
      "lastInboundMessageAt": { $ne: null }
    }
  },
  {
    $lookup: {
      from: "Users",
      localField: "users",
      foreignField: "_id",
      as: "users"
    }
  },
  {
    $match: {
      "users.owners": { $oid: "650825bcbf6db135710001fb" }
    }
  },
  {
    $count: "_totalCount"
  }
]);

env data:

php 8.3
libbson bundled version => 1.28.0
libmongoc bundled version => 1.28.0
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => enabled
libmongoc SASL => enabled
libmongoc SRV => enabled
libmongoc compression => enabled
libmongoc compression snappy => enabled
libmongoc compression zlib => enabled
libmongoc compression zstd => enabled
libmongocrypt bundled version => 1.11.0
libmongocrypt crypto => enabled
libmongocrypt crypto library => libcrypto
mongodb.debug => no value => no value

   requires
composer-runtime-api ^2.0
ext-hash *
ext-json *
ext-mongodb ^1.20.0
php ^8.3
psr/log ^1.1.4|^2|^3
symfony/polyfill-php80 ^1.27
symfony/polyfill-php81 ^1.27

requires (dev)
doctrine/coding-standard ^12.0
rector/rector ^1.1
squizlabs/php_codesniffer ^3.7
symfony/phpunit-bridge ^5.2
vimeo/psalm ^5.13

thanks

pd. on mongodb 7 I'm having same issue

pd2. is working if I put an unwind before lookup for users field (is an array of user ids)

jmikola commented 1 month ago

pd2. is working if I put an unwind before lookup for users field (is an array of user ids)

Does this mean the issue is completely resolved. We can close if so.

However this query if I run it from a mongo client works fine, so I suspect that there is a problem with the driver or the php library.

There is not much logic in the PHP extension or library that would interfere with execution of an aggregation pipeline (unless it was a bug in the new query builder). It's more likely that the issue is in constructing the pipeline itself, which the driver appends to an outgoing aggregate command. In that case, it'd be helpful to see actually PHP code to investigate instead of just the mongosh example shared in the OP.

Also, "query keeps running" in the title and "when executed remains undefined" in the OP text seems contradictory. If the query is still executing on the server you wouldn't get any response in the PHP driver and likely hit a socket timeout. But if the library's Collection::aggregate() method does return a cursor with no results, that implies the query has completed and returned an unexpected result.