[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eThis document explains how to create and test membership queries, which are used to define which users are included in dynamic groups.\u003c/p\u003e\n"],["\u003cp\u003eMembership queries are written using Common Expression Language (CEL) and consist of a target field and a condition, such as checking for specific values in user attributes like addresses or organizational units.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eexists()\u003c/code\u003e function is used in CEL to check for the presence of specific values within list-type fields, and you can make queries case-insensitive by using the \u003ccode\u003eequalsIgnoreCase()\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e!\u003c/code\u003e operator allows for negating conditions to exclude users with certain attributes, although certain combinations with the \u003ccode\u003eexists()\u003c/code\u003e macro are not supported.\u003c/p\u003e\n"],["\u003cp\u003eThe simple query builder tool is recommended to test your queries, and custom user attributes can be used in queries but must be entered directly into the CEL text box.\u003c/p\u003e\n"]]],[],null,["# Formulating and testing a membership query\n==========================================\n\nThis document explains how to formulate and test your membership queries to\nensure the correct users are returned.\n\nBefore you begin\n----------------\n\nPerform the following tasks before proceeding with the information on this page:\n\n- Read the [Groups API overview](/identity/docs/overview).\n\n- Read the [dynamic groups overview](/identity/docs/concepts/overview-dynamic-groups).\n\n- [Set up the Groups API](/identity/docs/how-to/setup).\n\nFormulating and testing your query\n----------------------------------\n\nMembership queries take the form of *`target_field condition`* , and are\nformatted as\n[Common Expression Language](https://opensource.google/projects/cel) (CEL)\nexpressions. See\n[Valid user attributes for dynamic groups](/identity/docs/how-to/dynamic-groups-attributes)\nfor the list of `User` resource fields that can be used.\n\n### Sample queries\n\nThe following example shows how to check for a specific value in a\n`user.addresses` field using the `exists()` CEL function: \n\n user.addresses.exists(ad, ad.locality=='Sunnyvale')\n\nThe `exists()` macro is used to create membership queries for fields of type\nlist.\n\nHere is an example of checking multiple values, (building) `area` and\n`building_id`, in a single list entry: \n\n user.locations.exists(loc, loc.area=='Sunnyvale' && loc.building_id=='Building 1')\n\nThe following example shows how to find users who are part of a given\norganizational unit:\n\n- **All direct users under a given org unit:** `user.org_unit_id==orgUnitId('03ph8a2z1enx4lx')`\n- **All direct and indirect users under a given org unit:** `user.org_units.exists(org_unit, org_unit.org_unit_id==orgUnitId('03ph8a2z1khexns'))`\n\n**Note:** You can make a query case-insensitive by adding `equalsIgnoreCase()` to the value being queried. For example, a query on username would be written as: \n\n user.name.value.equalsIgnoreCase('jOhn DoE')`\n\n### Negation in queries\n\nYou can use a NOT `!` operator in membership queries to exclude users with\ncertain attributes. Here are some examples:\n\n- All users who do not belong to a given organizational unit:\n\n !(user.org_unit_id==orgUnitId('03ph8a2z1enx4lx'))\n\n- All users who do not belong to the `Marketing` organization:\n\n !user.organization.exists(org, org.title = \"Marketing\")\n\nSome combinations of the NOT `!` operator and the `exists()` macro are not supported:\n\n- Any NOT `!` query using the `exists()` macro and using the AND `&&` operator within it:\n\n !user.organization.exists(org, (org.title == \"Cloud\" && org.department == \"Sales\"))\n\n- Any query using the `exists()` macro with the NOT `!` operator inside it:\n\n user.organization.exists(org, (org.title == \"Cloud\" || !(org.department == \"Sales\")))\n\nTesting membership queries\n--------------------------\n\nTo test your membership queries, you can use the\n[simple query builder](https://admin.google.com/ac/dgpreview). It allows you\nto use drop-down menus and text fields to enter and test a query. After you\nare sure the membership query returns the correct results, you can create the\ncorresponding CEL queries with the Groups API.\n\n### Custom user attributes\n\n| **Note:** Custom attribute support is currently in beta. The query will fail if the `schemaName` or `fieldName` in the query contains a hyphen (-).\n\n[Custom user attributes](https://developers.google.com/admin-sdk/directory/v1/guides/manage-schemas)\ncan used in dynamic group queries as long as the custom schema already exists in\n`user.custom_schemas`, but they are not supported in the simple query builder\ndrop-downs. To test a query for a custom user attribute, enter it in CEL query\nformat directly into the **Enter cel query** text box. A sample custom schema\nlooks like this: \n\n {\n \"schemaName\": \"employmentData\",\n \"fields\": [\n {\n \"fieldName\": \"EmployeeNumber\",\n \"fieldType\": \"STRING\",\n \"multiValued\": \"false\"\n },\n {\n \"fieldName\": \"JobFamily\",\n \"fieldType\": \"STRING\",\n \"multiValued\": \"true\"\n }\n ]\n }\n\n**Single valued field**\n\n`user.custom_schemas.employmentData.EmployeeNumber == 'value'`\n\n**Multi-valued field**\n\n`user.custom_schemas.employmentData.JobFamily.exists(fld, fld == 'value')`\n\nWhat's next\n-----------\n\nAfter you test your membership query, you can use it when creating a group. For\nmore information, refer to\n[Creating a dynamic group](/identity/docs/how-to/create-dynamic-groups)."]]