Biruwon
Biruwon

Reputation: 394

Sort by reference document in Doctrine MongoDB ODM

I'm using DoctrineMongoDBOBundle with Symfony2.

I've a Document Product which has an annotation referenceOne to other Document Price.

I want to sort by price when I fetch with queryBuilder.

$qb = $dm->createQueryBuilder('MyBundle:Product')
->field('geocoordinates')
->near('lat','lon')
->sort('hasPrice','desc')

But this doesn't works. Perhaps for the use of near?

It depends of toString() method of Document Price?

Regards.

Upvotes: 0

Views: 2021

Answers (2)

jmikola
jmikola

Reputation: 6922

In this case, hasPrice looks like it corresponds to a method, that perhaps checks whether the price reference is null or not. When referring to fields in ODM queries, names of class properties may be translated to the MongoDB field names (if they differ), but there is no support for method evaluation. Sammaye was correct that Doctrine does no client-side aggregation or sorting.

As an alternative, you may be able to sort on the price.$id field, which should group nonexistent values together in the results. Similarly, you could use the $exists operator to match/exclude based on whether a document was actual referenced. References in ODM (excluding the "Simple" type) are stored as MongoDBRef instances, which translate to objects with $id, $ref, and $db fields. As a result, you can query on those values just like any other field in your document.

Upvotes: 0

Sammaye
Sammaye

Reputation: 43884

I've a Document Product which has an annotation referenceOne to other Document Price.

There are no joins in MongoDB and I do not believe Doctrine does client side aggregation and sorting here. As such this wouldn't work anyway.

However sorting will work on a $near command ( http://docs.mongodb.org/manual/reference/operator/near/ ) which is what Doctrine should be using in this case, here you can see explicit support for $near being identified through the command you are using: https://github.com/doctrine/mongodb/commit/59f73cde2c15d171ff39afbf46c1a1339a51048c so your problem is the linked document, MongoDB has no JOINs.

Upvotes: 2

Related Questions