---
updatedAt: 2025-07-17T10:44:30.000Z
agentTools:
  projectIndex: https://docs.appery.io/llms.txt
---

# query (search)

Collection.query(dbId, collectionName[, params][, token])

Queries (searches) are a list of objects from a specified collection.

\#Parameters
The method has the following parameters:

<HTMLBlock>{`
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr>
  <th style="border: 1px solid #ddd; padding: 8px;">Argument</th>
  <th style="border: 1px solid #ddd; padding: 8px;">Description</th>
  <th style="border: 1px solid #ddd; padding: 8px;">Required</th>
</tr>
</thead>
<tbody>
<tr>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>dbApiKey</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>String with database API key.</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>Yes.</p>
</td>
</tr>
<tr>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>collectionName</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>String with name of database collection.</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>Yes.</p>
</td>
</tr>
<tr>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>params</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>JSON object that contains request parameters:</p>
<p> <em>criteria</em> – JSON – queries the object that was built in accordance with the mongo querying documentation.
 <em>skip</em> – integer – parameter indicating how many objects should be skipped before including objects into the response.</p>
<ul>
<li>limit* – integer – parameter indicating how many objects should be included in response.
 <em>count</em> – boolean – parameter indicating whether to include or not include the number of selected objects in the response;
 <em>sort</em> – string – list of fields names split by commas that indicate order of objects in response.
 <em>include</em> – string – parameter indicating whether the referenced object should or shouldn’t be included.
 <em>proj</em> – JSON – projection object that was built in accordance with the documentation of mongo querying.</li>
</ul>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>Yes.</p>
</td>
</tr>
<tr>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>token</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>String with user token or database Master key.</p>
</td>
  <td style="border: 1px solid #ddd; padding: 8px;"><p>No. Only required if the collection is secured.</p>
</td>
</tr>
</tbody>
</table>
`}</HTMLBlock>

> 📘 **Request Limitation**
> By default, 100 records are returned from the database.
> To increase the query up to 1500 records, set the limit field in the params argument: `var params = { limit:1500 }`.

\#Response
A JSON object with a list of objects contained in the collection according to the request:

```json
[
  {
    "_id": <object_id>,
    <colName>: <value>,
    "_createdAt": {"$date": <create_date>},
    "_updatedAt": {"$date": <update>}
  },
  ...
]
```

\#Examples

This example shows how to search in column **lastName** for **Lee**.

```javascript
var dbApiKey = "c33010af-f263-4443-9897-c2ffaf522956";
var params = {};
params.criteria = {
   "lastName": {
      "$eq": "Lee"
   }
};
var result = Collection.query(dbApiKey, "Student", params);
```

```json
[
	{
		"major": {
			"name": "English"
		},
		"isTransferStudent": true,
		"courses": [
			300,
			305
		],
		"lastName": "Lee",
		"firstName": "Anna",
		"_id": "57a52796e4b03647ddae32a5",
		"_updatedAt": {
			"$date": "2016-08-08T19:11:36.301Z"
		},
		"toAddress": {
			"collName": "Address",
			"_id": "57a5199be4b03647ddae3297"
		},
		"_createdAt": {
			"$date": "2016-08-05T23:56:06.051Z"
		}
	},
	{
		"isTransferStudent": true,
		"_updatedAt": {
			"$date": "2016-08-08T20:46:23.833Z"
		},
		"toAddress": {
			"collName": "Address",
			"_id": "57a52992e4b03647ddae3297"
		},
		"lastName": "Lee",
		"courses": [
			100,
			101
		],
		"firstName": "Sarah",
		"_id": "57a8ed82e4b081a83615fc33",
		"major": {
			"name": "Chemistry"
		},
		"_createdAt": {
			"$date": "2016-08-08T20:37:22.486Z"
		}
	}
]
```

The following examples is the same as the first one but now with a session token:

```javascript
var user = DatabaseUser.login("c33010af-f263-4443-9897-c2ffaf522956", "admin", "123");

var dbApiKey = "c33010af-f263-4443-9897-c2ffaf522956";
var params = {};
params.criteria = {
   "lastName": {
      "$eq": "Lee"
   }
};
var result = Collection.query(dbApiKey, "Student", params, user.sesssionToken);
```

This example shows how to count the number of objects in a collection:

```javascript
var dbApiKey = "c33010af-f263-4443-9897-c2ffaf522956";
var result = Collection.query(dbApiKey, "Student", {"count": 1});
```

```json
[
  {
    "count": 20
  }
]
```

This example shows how to search by the built-in **\_updatedAt** column:

```javascript
var dbApiKey = "c33010af-f263-4443-9897-c2ffaf522956";
var now = new Date();
var params = {};
params.criteria = {
   "_updatedAt": {
      "$lt": { "$date": now.toISOString() }
   }
};
var result = Collection.query(dbApiKey, "Goods", params);
Apperyio.response.success(result, "application/json");
```

```text
// Returns all objects updated before 'now'.
```

You can also use the built-in **\_createdAt** column the same way.

The following examples hows how to search by a custom field of the ***Date*** type. Note that the syntax is slightly different than when using the built-in date columns.

> 📘 ISO Format
> When using one of the built-in data types in a query (**\_createdAt**, **\_updatedAt**), the date value must be formatted in ISO format. When using a custom column with ***Date*** type, using ISO format is not required.

```javascript
var dbApiKey = "c33010af-f263-4443-9897-c2ffaf522956";
var params = {};
params.criteria = {
"startTime": {
"$gt": "2016-12-31 00:00:00.000"
}
};
var result = Collection.query(dbApiKey, "Goods", params);
Apperyio.response.success(result, "application/json");
```

```text
// Returns all items where startTime is greater than Dec 31, 2016
```

The following examples uses two date columns:

```javascript
var dbApiKey = "c33010af-f263-4443-9897-c2ffaf522956";
var params = {};
params.criteria = {
  "$and": [{
    "startTime": {
      "$gt": "2016-12-31 00:00:00.000"
    }
  }, {
    "endTime": {
      "$lt": "2017-02-01 00:00:00.000"
    }
  }]
};
var result = Collection.query(dbApiKey, "Goods", params);
Apperyio.response.success(result, "application/json");
```

```text
// Returns all items where time is between December 31, 2016 and February 1, 2017
```