简体   繁体   English

按Doctrine MongoDB ODM中的参考文档排序

[英]Sort by reference document in Doctrine MongoDB ODM

I'm using DoctrineMongoDBOBundle with Symfony2. 我正在使用DoctrineMongoDBOBundle和Symfony2。

I've a Document Product which has an annotation referenceOne to other Document Price. 我有一个文档产品,它有一个注释referenceOne到其他文档价格。

I want to sort by price when I fetch with queryBuilder. 我想用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? 它取决于Document Price的toString()方法?

Regards. 问候。

I've a Document Product which has an annotation referenceOne to other Document Price. 我有一个文档产品,它有一个注释referenceOne到其他文档价格。

There are no joins in MongoDB and I do not believe Doctrine does client side aggregation and sorting here. MongoDB中没有连接,我不相信Doctrine在这里进行客户端聚合和排序。 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. 然而,排序将在$near命令( http://docs.mongodb.org/manual/reference/operator/near/ )上运行,这是Doctrine在这种情况下应该使用的,在这里你可以看到明确支持$near通过您正在使用的命令识别: https//github.com/doctrine/mongodb/commit/59f73cde2c15d171ff39afbf46c1a1339a51048c因此您的问题是链接文档,MongoDB没有JOIN。

In this case, hasPrice looks like it corresponds to a method, that perhaps checks whether the price reference is null or not. 在这种情况下, hasPrice看起来对应于一个方法,可能会检查price引用是否为null。 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. 在引用ODM查询中的字段时,可以将类属性的名称转换为MongoDB字段名称(如果它们不同),但不支持方法评估。 Sammaye was correct that Doctrine does no client-side aggregation or sorting. Sammaye是正确的,Doctrine没有客户端聚合或排序。

As an alternative, you may be able to sort on the price.$id field, which should group nonexistent values together in the results. 作为替代方案,您可以对price.$id进行排序price.$id字段,它应该在结果中将不存在的值组合在一起。 Similarly, you could use the $exists operator to match/exclude based on whether a document was actual referenced. 同样,您可以使用$ exists运算符来匹配/排除文档是否实际引用。 References in ODM (excluding the "Simple" type) are stored as MongoDBRef instances, which translate to objects with $id , $ref , and $db fields. ODM中的引用(不包括“简单”类型)存储为MongoDBRef实例,它们转换为具有$id$ref$db字段的对象。 As a result, you can query on those values just like any other field in your document. 因此,您可以像文档中的任何其他字段一样查询这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM