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

Create "notReferences" query ? #1769

Open
lucalbert opened this issue Apr 3, 2018 · 3 comments
Open

Create "notReferences" query ? #1769

lucalbert opened this issue Apr 3, 2018 · 3 comments
Labels
Projects

Comments

@lucalbert
Copy link

Hello everyone,

In my application, I have a reference (One) between 2 entities:

namespace App\Document

class Offer
{
  /**
   * @MongoDB\ReferenceOne(targetDocument="Customer")
   */
  protected $customer;
}

When I want to retrieve all the references, no problem:

$repository = $container->get('doctrine_mongodb')->getManager()->getRepository(Offer::class);
$offers = $repository->findBy (['customer' => $customer]);

// or
$dm = $container->get('doctrine_mongodb')->getManager();
$qb = $dm->createQueryBuilder(Offer::class);

$offers = $qb
            ->field('customer')->references($customer)
            ->getQuery()
            ;

// or

$offers = $qb
             ->field('customer')->equals($customer)
             ->getQuery()
         ;

In both cases, the query is the same:

db.offers.find({ "customer.$id": ObjectId("5abe0295b4bfd00008089c73") });

Now, I want to get all the documents that do not have the reference !
When i try this :

$dm = $container->get ('doctrine_mongodb')->getManager();
$qb = $dm->createQueryBuilder(Offer::class);

$offers = $qb
             ->field('customer') ->notEqual($customer)
             -> getQuery()
         ;

I get the following error: "Catchable Fatal Error: Object of class MongoDBODMProxies_CG_\App\Document\Customer could not be converted to string"

If i try this:

$offers = $qb
            ->field('customer.$id')->notEqual($customer->getId())
            ->getQuery()
        ;

I get the following query:

db.offers.find({ "customer.$id": { "$ne": "5abe0295b4bfd00008089c73" } }); // It's not a ObjectId

How to launch such a query?

@malarzm
Copy link
Member

malarzm commented Apr 3, 2018

Given querying by customer.$id you need to explicitly tell ODM that there's ObjectId inside, so:

>field('customer.$id')->notEqual(new \MongoId($customer->getId()))

Offhand, given we have references and includesReferenceTo helper methods it might be feasible to provide their counterparts. Marking as feature.

@malarzm malarzm added the Feature label Apr 3, 2018
@malarzm malarzm added this to the 2.x milestone Apr 3, 2018
@lucalbert
Copy link
Author

Nice @malarzm!

Thanks you.

@alcaeus alcaeus added this to 2.1 in Roadmap Jul 13, 2018
@alcaeus alcaeus moved this from 2.1 to 2.x in Roadmap Nov 5, 2019
@jcaillot
Copy link

jcaillot commented Mar 21, 2020

my 55 cents :

// "$nor performs a logical NOR operation on an array of **ONE** or more query expression"
// https://docs.mongodb.com/manual/reference/operator/query/nor/

$qb->addNor($qb->expr()->field('field_name')->references($document));

@alcaeus alcaeus removed this from the 2.x milestone Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Roadmap
2.x (future scope)
Development

No branches or pull requests

4 participants