Skip to content

Searching for documents (persistent.findDocuments)

You can use persistent.findDocuments to find documents that are made from a specific document definition.

You can use the following method in your script

persistent.findDocuments(ddName, queryObject, queryOptions);
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

Read here how you can find the ddName

Query Parameter Type Description
sort Int Used for the soring 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
skip Int Number of results to skip at the beginning.
fields Int 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 -1)
limit Int Maximum number of results to return.

Sort query parameter example
The field {"projectNumber": 1} sorts the documents ascending based on the project number

Fields query parameter example

{
    "projectNumber": 1,
    "description": -1
}

ensures that the field projectNumber is included while the field description is excluded from the result.

Limit query parameter example

{
    "sort": { "name": -1},
    "limit": 100,
    "fields": {"name": 1,"price": -1}
}

Examples

The following method allows you to search for documents of the type project for which the projectNr field is equal to 12345.

persistent.findDocuments('project', {"projectNr":"12345"});

It is possible to use several searching criteria simultaneously:

persistent.findDocuments('project', {"projectNr":"12345", "projectName": "Test Project"});

You can also search for documents that matches at least one of the searching criteria:

persistent.findDocuments('project', {$or:[{"code":"12345"}, {"name": "Test Project"}]});

You can also query documents using the queryOptions parameter:

persistent.findDocuments('project', {"active": true}, {
    sort: {'projectNumber': -1},
    limit: 100,
    fields: {'projectName': 1, 'description': -1}
});

This will return the documents that are active. It will sort these documents ascending, based on the field projectNumber and limits the result to 100 documents. The field projectName is included in the result while the field description is excluded.

The result is an array of Documents that match the search criteria. You will be shown the full documents:

[
    {   "_id":"2FwMgqxxPv6i4j8rc",
       "_documentDefinitionId":"iaK8uBLxDqmTJzYdL",
       "_environmentId":"a83rh4aX5nzBsGL3S",
       "accessGroups":[],
       "code":"12345",
       "name":"constructing highway",
       "companyId":"a6PbuPzkoRp3Czqcv",
       "externalId":"bdr:200¡adm:05¡prj:6"
    },
    {   "_id":"syL7ExGTk72cvrs7J",
       "_documentDefinitionId":"iaK8uBLxDqmTJzYdL",
       "_environmentId":"a83rh4aX5nzBsGL3S",
       "accessGroups":[],
       "code":"7890",
       "name":"Test Project",
       "companyId":"a6PbuPzkoRp3Czqcv",
       "externalId":"bdr:200¡adm:05¡prj:6"
    }
]

Tip: by using the console console.log('a message', variable) you can find the result in the logs.

//Start of the script
var projectNumber = persistent.findDocuments('project', {"projectNr":"12345"});
console.log('Give me the documents with projectnumber 12345', projectNumber)
//End of the script

Limits

The persistent.findDocuments method is subject some limits. Click here to find out more.