doctrine/mongodb-odm — Issue #204: "No mapping found for field at" for "elemMatch" with embedded
Author: cystbear
Created: 2011-12-08 19:07:11+00:00
State: open

I have found this @tecbot commit
https://github.com/doctrine/mongodb-odm/commit/ee37d65fb73a29c7d80ee8f2adc48665f59f7030

and want ask. I have next code

```
$qb = $this->createQueryBuilder();
$query = $this->createIsActiveQuery()
->field('userPrograms')
->elemMatch(
$qb->expr()
->field('program.$id')->equals(new \MongoId($program->getId()))
->field('isEnabled')->equals(true)
);

return $query;
```

here I am at userRepository, user have `userPrograms` field (embedded many).
`userPrograms` have reference one `program` (thus have `program.$id`) and `isEnabled`

After upgrade `doctrine-common` `doctrine-mongodb` and `doctrine-mongodb-odm` this code stop works with message
```
No mapping found for field 'program' in class 'Application\UserBundle\Document\User'
```

I have change first version of query for this:

```
$qb = $this->createQueryBuilder();
$query = $this->createIsActiveQuery()
->field('userPrograms')
->elemMatch(
$qb->expr()
->field('userPrograms.program.$id')->equals(new \MongoId($program->getId()))
->field('userPrograms.isEnabled')->equals(true)
);

return $query;
```

with this changes query works, at least not throw any error.
So my questions are:
1) Did I made correct changes?
2) Could you please explain changes?
3) Did previous version of query works properly?

=================
@tecbot answered to me

Hey,

no problem, but you can write me a message on github. You find this on the left side in your inbox ("Compose Message") and than you can write to my nickname ;).

I think your last query doesn't work, because your query looks like this in the end:
db.foo.find({userPrograms: { $elemMatch: { "userPrograms.isEnabled": true } }}
Correct is this verison:
db.foo.find({userPrograms: { $elemMatch: { "isEnabled": true } }}

The problem is in the pervious versions there was no check in embedded expressions, so after my changes it validates too. But the problem is in your case that the $elemMatch operator is a special operator wich don't need the parent fieldname. Currently, I have found no solution for this.

@jwage: Maybe you find a solution for this problem?

Btw: you dont need to write:
->field('userPrograms.program.$id')->equals(new \MongoId($program->getId()))
you can write now:
->field('userPrograms.program.id')->equals($program->getId())