---
title: Query multiple models
summary: Run one query over several versions, tell the results apart by version, and select the models under a folder over HTTP.
order: 50
languages: [javascript, java, csharp, php]
terms: [qid, vid, version, node]
see-also: [how-to/query/run-a-bql-query, how-to/models/list-the-tree, explanation/identifiers, reference/bql]
---

# Query multiple models

For a project view that spans the architectural, structural and services models, or any question across a set of files. A query takes a list of [[term:version|versions]] and answers with one merged list of records; the [[term:qid|qid]] of each record says which version it came from.

## Before you start

- A login with the `query:run` scope and read rights on every version; a delegated token must name all of them ([[page:how-to/auth/create-a-versions-token]]).
- The version UUIDs ([[page:how-to/models/list-the-tree]]).

## 1. Pass several versions

```code-group {#query} classic
```javascript
const objects = await client.bim.query([BIM_WORKS_FIRST_MODEL, BIM_WORKS_SECOND_MODEL],
	"GET qid, attribute.Name WHERE type = IFCDOOR");
```
```java
ArrayNode objects = client.bim.query("GET qid, attribute.Name WHERE type = IFCDOOR",
		UUID.fromString(BIM_WORKS_FIRST_MODEL), UUID.fromString(BIM_WORKS_SECOND_MODEL));
```
```csharp
ISet<Guid> versions = Collections.CreateHashSet(Guid.Parse(BIM_WORKS_FIRST_MODEL), Guid.Parse(BIM_WORKS_SECOND_MODEL));
JsonElement objects = client.Bim.Query2(versions, "GET qid, attribute.Name WHERE type = IFCDOOR");
```
```

```sdk-example
operation: Query
body: |
  {
    "models": [
      {"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL},
      {"type": "VersionModelIdentifier", "uuid": BIM_WORKS_SECOND_MODEL}
    ],
    "query": "GET qid, attribute.Name WHERE type = IFCDOOR"
  }
```

The same methods as [[page:how-to/query/run-a-bql-query]]: `query` on [[js:@bimworks/jssdk/Bim|Bim]], `BimApi.query` on [[java:nl.bimbase.bimworks.client.apis.BimApi|BimApi]], `Bim.Query2` on [[dotnet:BIM.works_SDK.Bim|Bim]], `IFCApi::query` in [[page:reference/sdk/php]], [[openapi:Query]] over HTTP.

## 2. Tell the results apart

Every record's `qid` starts with the [[term:vid|vid]] of its version. Group by it to get per-model results:

```code-group {#split}
```javascript
const perVersion = new Map();
for (const object of objects) {
	const vid = object.qid.vid;
	perVersion.set(vid, (perVersion.get(vid) || 0) + 1);
}
```
```java
Map<String, Integer> perVersion = new HashMap<>();
for (JsonNode object : objects) {
	String qid = object.get("qid").asText();
	String vid = qid.substring(0, qid.lastIndexOf('-'));   // a vid is a UUID with dashes, the oid follows the last dash
	perVersion.merge(vid, 1, Integer::sum);
}
```
```

Aggregates run over the union: `GET type, COUNT(qid) GROUP BY type` counts across all versions, and `SUM`, `MIN` and `MAX` likewise. Relations do not cross model boundaries: `buildingStorey.attribute.Name` is each object's storey in its own model.

## 3. Select models by folder over HTTP

The HTTP [[openapi:Query]] operation accepts, besides version identifiers, a `NodeModelIdentifier`: the models of a [[term:node|node]], with `recursive: true` every model below a folder. This spares the caller a listing when a folder holds a project:

```try-it
operation: Query
method: POST
path: /openapi/ifc/Query
body: |
  {
    "models": [{"type": "NodeModelIdentifier", "uuid": "<folder uuid>", "recursive": true}],
    "query": "GET type, COUNT(qid) GROUP BY type"
  }
```

Use it with care on large trees: every model below the folder is loaded for the query. The SDKs take version UUIDs only; list the folder first ([[page:how-to/models/list-the-tree]]).

## Check

`queryCount` ([[page:how-to/query/count-results]]) of `GET qid WHERE type = IFCDOOR` on both versions equals the sum of the two single-version counts, and the per-version map from step 2 has exactly two keys, the two vids.
