繁体   English   中英

Doctrine ODM查询生成器 - 查找集合为空的位置

[英]Doctrine ODM Query Builder - Find where collection is empty

我正在尝试使用doctrine ODM查询构建器创建查询,其中引用的关联( eventListeners不为空 - 或者集合具有一个或多个项目。

查询:

$qb = $om->createQueryBuilder(FormService::ENTITY_CLASS_NAME_FORM);
$query = $qb->field('website.$id')->equals(new \MongoId($website->getId()))
            ->field('status.name')->equals(FormService::STATUS_PUBLISHED)
            ->field('eventListeners')->notEqual(array());
            ->getQuery();
$results = $query->execute();

我在尝试使用API​​方面一直很有创意; 此行显然不正确,因为它仍然返回所有文档,无论如何

->field('eventListeners')->notEqual(array());

我可以在文档中看到你可以使用field('eventListeners')->size(3); 但我事先并不知道收集的大小应该是多少。

如何使用Doctrine ODM查询非空集合?

它可能不是实现这一目标的最佳方式,但它确实有效:

$qb = $om->createQueryBuilder(FormService::ENTITY_CLASS_NAME_FORM);
$query = $qb->field('website.$id')->equals(new \MongoId($website->getId()))
            ->field('status.name')->equals(FormService::STATUS_PUBLISHED)
            ->field('eventListeners.0')->exists(true)
            ->getQuery();

$results = $query->execute();

这假设它是基于0的索引Collection而不是Hash

我意识到你可以在DB.find({eventListeners: {$not: {$size: 0}}})中做DB.find({eventListeners: {$not: {$size: 0}}}) ,但我不确定如何在ODM查询构建器中正确构建它。

在查看odm文档之后,我不确定它是否可行。

另一种方法是使用$where函数: http//docs.mongodb.org/manual/reference/operator/query/where/

暂无
暂无

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

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