简体   繁体   English

教义Mongo ODM在阵列中找到

[英]Doctrine Mongo ODM find in array

I'd like to build a query that filters for the elements of the roles array. 我想建立一个查询,以过滤角色数组的元素。 I need the users who have the role admin or the role user. 我需要具有角色admin或角色用户的用户。 This is (a part of) my document: 这是我的文档(的一部分):

{
    "username": "test_joe",
    "email": "joe@joe.joe",
    "enabled": true,
    "roles": [
        "admin",
        "user",
        "guest"
    ]
}

I tried something like this, but this is not the correct one: 我尝试了类似的方法,但这不是正确的方法:

$qb = $dm->getRepository('AppBundle:User')->createQueryBuilder('user')
        ->select('email');

$qb->addOr($qb->expr()->field('roles.admin')->exists(true));
$qb->addOr($qb->expr()->field('roles.user')->exists(true));

There is no field roles.admin in your document. 您的文档中没有字段roles.admin It's field "roles" with array of values. 它是具有值数组的"roles"字段。 MongoDB matching is polymorphic on arrays so you can match just like you having field "roles": "admin" So you don't need exists but something like ->field('roles')->equals('admin') MongoDB匹配在数组上是多态的,因此您可以像具有字段"roles": "admin"一样进行匹配,因此不需要存在,但是类似->field('roles')->equals('admin')

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

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