# How to Query using both Mapkeys and Mapvalues?

**URL:** <https://discuss.aerospike.com/t/how-to-query-using-both-mapkeys-and-mapvalues/4877>\
**Category:** Query & Indexing\
**Created:** [January 23, 2018, 3:54am UTC](https://discuss.aerospike.com/t/how-to-query-using-both-mapkeys-and-mapvalues/4877 "2018-01-23T03:54:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bxcodec](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/bxcodec/32/939_2.png) [@bxcodec](https://discuss.aerospike.com/u/bxcodec)\
**Post date:** [January 23, 2018, 3:54am UTC](https://discuss.aerospike.com/t/how-to-query-using-both-mapkeys-and-mapvalues/4877/1 "2018-01-23T03:54:47Z")

</div>

Hello,

I’m new to Aerospike. And would try it with our new feature in our company.

We have data like this

```
[
 attribues: map {
   age:19,
   gender: male,
 },
 atribute: map {
   age:19,
   gender female,
},
 attributes: map {
   gender: male,
 },
 attributes: map {
   age:19,
 }
]

```

Then we want to querying based on attributes. Example:

1. We want to get data that has attributes: `gender: male` only, then it should return only one, but it sent 3 items.

2. We want to get data the has attributes: `age:19` only, it should return only one, but it return 2 items.

Am I doing something wrong ? I don’t find any documentation about querying like this, is this query supported?

---

<div class="post-metadata">

**Author:** ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)\
**Post date:** [January 25, 2018, 2:55am UTC](https://discuss.aerospike.com/t/how-to-query-using-both-mapkeys-and-mapvalues/4877/2 "2018-01-25T02:55:56Z")

</div>

[AQL](https://www.aerospike.com/docs/tools/aql/index.html) is not a query language (like SQL), it’s a CLI tool you can use for admin of things such as indexes and UDF modules, or for basic data browsing. Keep that in mind when you try to create more complex queries.

Aerospike supports secondary-index [queries](https://www.aerospike.com/docs/guide/query.html) of a limited nature. Because Aerospike does not have a schema, your [secondary index](https://www.aerospike.com/docs/architecture/secondary-index.html) needs to define not just the bin it’s being built on, but also declare the data type of the data (with integer, string, and geospatial supported) and the data ‘container’ (none for scalar data, list, map-keys or map-values). Only records that have data in the declared bin, **with** the declared data type **and** container will be indexed by the specified index. You can have multiple indexes on your records.

So, you should be able to build an INTEGER index on MAPVALUES, and use that to find users within a specified integer range. You can then do further processing on the application side. If you look closely at the _queries_ link above, you’ll see that you can only use one predicate with a query. There is no support for the more complex WHERE clause you’re trying to use.

For complex predicates there is [predicate filtering](https://www.aerospike.com/docs/guide/predicate.html), which is very powerful way to build a multi-predicate filter that can look at both data (in the bins) and metadata (such as TTL, last-update-time, etc). Take a look at the [predicate filtering examples](https://github.com/aerospike/aerospike-client-java/blob/master/examples/src/com/aerospike/examples/QueryPredExp.java) in the Java client. You can use a predicate filter without a secondary index even (applied to a scan). AQL does not yet support predicate filtering in its syntax (but again, it’s a tool, not a query language).

There is another way to do what you’re looking for using [stream UDFs](https://www.aerospike.com/docs/udf/developing_stream_udfs.html), in case you’re using a language client that does not support predicate filtering. The ones that do are [Java](http://www.aerospike.com/apidocs/java/com/aerospike/client/query/PredExp.html), [C](http://www.aerospike.com/apidocs/c/d9/d10/as__predexp_8h_source.html), [C#](http://www.aerospike.com/apidocs/csharp/html/T_Aerospike_Client_PredExp.htm) and [Go](https://github.com/aerospike/aerospike-client-go/blob/master/predexp.go) clients. There are plenty of [examples on Stackoverflow](https://stackoverflow.com/search?q=%5Baerospike%5D+stream+udf).
