Doctrine MongoDB found by id

I use the odm mongo dogma, and I have class documents

class Thing { /** * @MongoDB\Id */ protected $id; /** * @MongoDB\ReferenceOne(targetDocument="Bundle1:Other") */ protected $other; } 

and

 class Other { /** * @MongoDB\Id */ protected $id; } 

therefore, in the database, the view is as follows:

 { "_id":ObjectId("43z758634875adf"), "other":ObjectId("38z287348d8se") } 

How can I now request things where the other is the given identifier?

  $dm=$this->mongo->getManager(); $answers=$dm ->createQueryBuilder('Bundle1:Thing') ->field('other')->equals("ObjectId(516c0061975a299edc44b419)") // <-- ? ->getQuery() ->execute()->count(); 

This causes an incorrect mango request

MongoDB query: {"Find": true, "query": {"other": "ObjectId (516c0061975a299edc44b419)"}, "fields": [], "DB": "maself", "collection": "thing"} [] []

When i use

-> field ('other') → equals ("516c0061975a299edc44b419")

the request is also invalid

MongoDB query: {"Find": true, "query": {"other": "516c0061975a299edc44b419"}, "fields": [], "DB": "maself", "collection": "thing"} [] [ ]

So how can I search for an item where the other id is equal to the id object?

+2
source share
1 answer

Try

 ->field('other')->equals(new \MongoId("516c0061975a299edc44b419")) 

ObjectId is an internal type for Mongo, introduced by \ MongoId () in PHP

(But I also answered in the first topic)

+4
source

Source: https://habr.com/ru/post/946963/


All Articles