Finding documents (persistent.findDocumentsV1)
You can use persistent.findDocumentsV1 to find documents based on a particular document definition. Unlike persistent.findDocuments, this method does not throw an error when the query object contains undefined values or is entirely undefined.
Syntax
persistent.findDocumentsV1(ddName, queryObject, queryOptions);
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| ddName | String | Yes | The name of the document definition. If the value is null, BizzStream will use the name of the document from which the script was started. |
| queryObject | object | No | The object with which you can specify the search criteria. This object has to be made in the Mongo query format |
| queryOptions | Object | No | The options that can be used in the Mongo query |
Query options
| Query Parameter | Type | Description | Validation Rules |
|---|---|---|---|
| sort | object | Used for the sorting order (default: natural order). This is an object that contains the fields and their sorting order. The value -1 represents a descending sorting order while the value 1 represents an ascending sorting order | Sort values must be either 1 or -1 |
| skip | number | Number of documents to skip at the beginning. | Must be an integer greater than or equal to 0 |
| fields | object | Dictionary of fields to return or exclude. This is an object that contains the fields and an indication of whether the field is included (value 1) or excluded (value 0) | Field projection values must be either 0 or 1 |
| limit | number | Maximum number of documents to return. When set to 0, the method applies the default upper limit of 800 documents. | Must be an integer between 0 and 800 |
Return value
An array containing the documents. If no documents can be found, a null value.
Note: Query options are validated according to the rules specified in the table above. If validation fails, an error will be thrown.
Examples
Basic query
The following method allows you to search for documents of the type project for which the projectNr field is equal to 12345.
const projectNumber = persistent.findDocumentsV1('project', {"projectNr":"12345"});
console.log('Give me the documents with projectnumber 12345', projectNumber)
Using limit and skip
Limit the number of results and skip the first few documents:
// Get only the first 10 documents
const limitedResults = persistent.findDocumentsV1('project', {"status":"active"}, {limit: 10});
// Skip the first 20 documents and get the next 10
const paginatedResults = persistent.findDocumentsV1('project', {"status":"active"}, {
skip: 20,
limit: 10
});
Using fields projection
Include or exclude specific fields from the results:
// Include only specific fields (1 = include, 0 = exclude)
const specificFields = persistent.findDocumentsV1('project', {"status":"active"}, {
fields: {
_id: 1, // Include document ID
projectNr: 1, // Include project number
name: 1, // Include name
description: 0 // Exclude description
}
});
// Exclude specific fields (all other fields will be included)
const excludeFields = persistent.findDocumentsV1('project', {"status":"active"}, {
fields: {
_hash: 0, // Exclude hash
_createdOn: 0, // Exclude creation date
_lastModifiedOn: 0 // Exclude last modified date
}
});
Using sort
Sort the results by specific fields:
// Sort by project number in ascending order
const sortedResults = persistent.findDocumentsV1('project', {"status":"active"}, {
sort: {
projectNr: 1 // 1 = ascending, -1 = descending
}
});
// Sort by multiple fields
const multiSortedResults = persistent.findDocumentsV1('project', {"status":"active"}, {
sort: {
status: 1, // First sort by status (ascending)
projectNr: -1 // Then by project number (descending)
}
});
Combining multiple options
Use multiple query options together:
const complexQuery = persistent.findDocumentsV1('project', {"status":"active"}, {
fields: {
_id: 1,
projectNr: 1,
name: 1,
description: 0
},
sort: {
projectNr: 1
},
skip: 10,
limit: 5
});