Server-Side Javascript ES6 API Reference
RMPCollections
Methods for managing Custom Mongo Collections
Create Collection
Creates a new Mongo collection
Syntax : RMPCollection.createCollection(collectionName, isPublic, isReadonly);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
isPublic | Boolean | Whether Mongo collection can be accessed publicly, without checking rights |
isReadonly | Boolean | Whether the Mongo collection can only be modified by an administrator of the customer account |
Returns : Collection successfully created or not
Type : Boolean
Call Example :
<@script env="js">
var f = RMPCollection.createCollection('col_rms_v3', false, false);
RMPData.setOutput(f);
</@script>
Drop Collection
Drop a Mongo collection
Syntax : RMPCollection.drop(collectionName);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of a collection |
Returns : Collection successfully removed or not
Type : Boolean
Call Example :
<@script env="js">
var f = RMPCollection.drop('col_rms_v1_new');
RMPData.setOutput(f);
</@script>
Rename Collection
Rename a Mongo collection
Note: Mongo collection names are unique with an account
Syntax : RMPCollection.renameCollection(oldCollectionName, newCollectionName);
Parameters :
Name | Type | Description |
---|---|---|
oldcollectionName | String | Current name of the Mongo collection |
newcollectionName | String | New name of the Mongo collection |
Returns : Renaming successful or not.
Type : Boolean
Call Example :
<@script env="js">
var f = RMPCollection.renameCollection('col_rms_v1', 'col_rms_v1_new');
RMPData.setOutput(f);
</@script>
Create Index
Creates indexes on Mongo collection
Syntax :
- RMPCollection.createIndex(collectionName, indexName, indexArray);
- RMPCollection.createIndex(collectionName, indexName, indexArray, unique);
- RMPCollection.createIndex(collectionName, indexName, indexArray, unique, ascending);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
indexName | String | Name of the Index |
indexArray | Array of strings | Index Array |
unique (optional) | Boolean | Whether the index shall observe uniqueness |
ascending (optional) | Boolean | The ordering |
Returns : The result of the create operation (true or false)
Type : Boolean
Call Example :
<@script env="js">
var idx = ['brand_id'];
var f = RMPCollection.createIndex('rms_car', 'rms_car_idx', idx);
RMPData.setOutput(f);
</@script>
Delete Index
Deletes an index on a Mongo collection
Syntax : RMPCollection.dropIndex(collectionName, indexName);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
indexName | String | Name of the Index |
Returns : The result of the delete operation (true or false)
Type : Boolean
Call Example :
<@script env="js">
var f = RMPCollection.dropIndex('rms_car', 'rms_car_idx');
RMPData.setOutput(f);
</@script>
List Index
List of all indexes on a Mongo collection
Syntax : RMPCollection.listIndexes(collectionName);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the collection |
Returns : List of indexes
Type : Array of maps
Data structure :
<@script env="js">
[{"name": "{String}", "keys": ["{String}, ..."], "unique": {Boolean}, "ascending": {Boolean} }, ...]
</@script>
Call Example :
<@script env="js">
var l = RMPCollection.listIndexes('rms_car');
RMPData.setOutput(l);
</@script>
Aggregate
Run an aggregation query over a Mongo collection
Note: Please make sure that you don't exceed the maximum memory limit when using the function.
Syntax : RMPCollection.aggregate(collectionName, pipelines);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
pipelines | Array of maps | An aggregation requires at least one pipeline |
Returns : JSON result of the aggregate operation
Type : Array of maps
Call Example :
<@script env="js">
var aggr = [ {'$match': {'brand_id':'bogdan_group'} }, { $group : { _id : '$name', totaldocs : { $sum : 1 } } } ];
var a = RMPCollection.aggregate('car_rms', aggr );
RMPData.setOutput(a);
</@script>
Find
Retrieve objects from a Mongo collection
Note: Please make sure that you don't exceed the maximum memory limit when using the function.
Syntax :
- RMPCollection.find(collectionName, query);
- RMPCollection.find(collectionName, query, offset);
- RMPCollection.find(collectionName, query, offset, limit);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the collection |
query | Map | Query to filter objects to be retrieved. |
offset (optional) | Integer | Number of leading items to be skipped. |
limit (optional) | Integer | Max number of items returned. |
Returns : List of objects
Type : Array of maps
Call Example :
<@script env="js">
var query = {};
var f = RMPCollection.find('car_rms', query);
RMPData.setOutput(f);
</@script>
Count Documents
Count documents verifying a condition in a Mongo collection
Syntax : RMPCollection.countDocuments(collectionName, query);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
query | Map | Mongo Query to filter objects to be retrieved |
Returns : The number of documents matching the query
Type : Long
Call Example :
<@script env="js">
var query = {};
var f = RMPCollection.countDocuments('car_rms', query);
RMPData.setOutput(f);
</@script>
Insert Object (insertOne)
Insert object in a Mongo collection
Syntax : RMPCollection.insertOne(collectionName, data);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
data | Map | Data to store in a collection |
Returns : The saved data
Type : Map
Call Example :
<@script env="js">
var payload = {"engine":"e1","brand_label":"Brand-1","name":"Name-1","brand_id":"b1"};
var f = RMPCollection.insertOne('car_rms', payload);
RMPData.setOutput(f);
</@script>
Insert Objects (insertMany)
Insert many objects in a Mongo collection
Syntax : RMPCollection.insertMany(collectionName, data);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
data | List (Map) | Data to store in a collection |
Returns : The saved data
Type : Array of Maps
Call Example :
<@script env="js">
var payload = [{"engine":"e2","brand_label":"Brand-2","name":"Name-2","brand_id":"b2"}, {"engine":"e3","brand_label":"Brand-3","name":"Name-3","brand_id":"b3"}];
var f = RMPCollection.insertMany('car_rms', payload);
RMPData.setOutput(f);
</@script>
Delete Objects
Remove fields from documents that matches with the given query
Syntax : RMPCollection.deleteMany(collectionName, query);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of a Mongo collection |
query | Map | Mongo query to filter objects to be retrieved |
Returns : Number of removed objects
Type : Integer
Call Example :
<@script env="js">
var query = {'brand_id':'b3'};
var num = RMPCollection.deleteMany('car_rms', query);
RMPData.setOutput(num);
</@script>
Update Objects
Update fields on a subset of the documents in a Mongo collection
Syntax : RMPCollection.updateMany(collectionName, data, query);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
data | Map | Data to be updated in a collection |
query | Map | Query to filter objects to be retrieved |
Returns : Number of updated fields
Type : Integer
Call Example :
<@script env="js">
var query = {'brand_id':'b2'};
var upd = {'$set':{'engine':'e2','brand_label':'Brand-2','name':'Name-2'}};
var num = RMPCollection.updateMany('car_rms', upd, query);
RMPData.setOutput(num);
</@script>
Update Field (updateMany)
Update fields on a subset of the documents in a Mongo collection
Syntax :
- RMPCollection.updateField(collectionName, query, data);
- RMPCollection.updateField(collectionName, query, data, multi );
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
query | Map | Mongo Query to filter objects to be retrieved |
data | Map | Data to be updated in a collection |
multi (optional) | Boolean | true if multiple documents have to be updated |
Returns : Number of updated fields
Type : Integer
Call Example :
<@script env="js">
var query = {"brand_id" : "bogdan_group"};
var upd = {"engine":"engine_3"};
var num = RMPCollection.updateField('car_rms', query, upd, true);
RMPData.setOutput(num);
</@script>
Update Field (updateOne)
Update a field on a subset of the documents in a Mongo collection
Syntax : RMPCollection.updateField(collectionName, query, data);
Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of the Mongo collection |
query | Map | Mongo Query to filter objects to be retrieved |
data | Map | Data to be updated in a collection |
Returns : Number of updated fields
Type : Integer
Call Example :
<@script env="js">
var query = {"brand_id" : "bogdan_group"};
var upd = {"engine":"engine_3"};
var num = RMPCollection.updateField('car_rms', query, upd, false);
RMPData.setOutput(num);
</@script>
Delete Field
Remove a field from documents that matches with the given query
Syntax :
- RMPCollection.deleteField(collectionName, query, fieldName);
- RMPCollection.deleteField(collectionName, query, fieldName, multi); * Parameters :
Name | Type | Description |
---|---|---|
collectionName | String | Name of a Mongo collection |
query | Map | Mongo Query to filter objects to be retrieved |
fieldName | String | Name of the field |
multi (optional) | Boolean | Whether the apply the change to multiple matched items |
Returns : Number of updated objects
Type : Integer
Call Example :
<@script env="js">
var query = {"name":"delete_me!"};
var num = RMPCollection.deleteField('car_rms', query, 'engine');
RMPData.setOutput(num);
</@script>
Import CSV
Import objects in a collection from a CSV file
Syntax :
- RMPCollection.importCSV(fileId, collectionName, options);
- RMPCollection.importCSV(fileId, collectionName, options, dropCollection);
Parameters :
Name | Type | Description |
---|---|---|
fileId | String | Reference of an uploaded file |
collectionName | String | Name of the Mongo collection |
options | Map | Options to be used during the call, all values are optional |
dropCollection (optional) | Boolean | if true , it creates the collection from scratch |
The "options" map can contain the following values:
Name | Type | Description |
---|---|---|
separator | String | Separator between two values (e.g. ; or , ). Default: , |
delimiter | String | Used to allow values containing a separator character (e.g. " or ' ),to include a delimiter in a value, use two delimiters (e.g. "" ). Default: " |
empty | String | Value to be used to represent an empty value (two separators in a row). Default: "" |
charset | String | Character set to be used to read the file content. Default: UTF-8 |
parse_numbers | Array | Names/indices of columns containing numerical values to be stored as Double. e.g ["age",3] will parse columns header "age" and the 4th column |
parse_longs | Array | Names/indices of columns containing long values to be stored as Long. e.g: ["age",3] will parse column header "age" and the 4th column. |
trim | boolean | Trim values before inserting them in the collection. Trimming also occurs before decoding numerical values. |
Throws : Uploaded file X doesn't exist.
Returns : The number of imported objects.
Type : Integer
Call Example :
<@script env="js">
var options = {};
var noOfRecords = RMPCollection.importCSV("30ad9a42-8a4e-48b3-b857-660979eb77dd", "csks", options);
RMPData.setOutput(noOfRecords);
</@script>
Import JSON
Import objects in a Mongo collection from a JSON file.
Syntax :
- RMPCollection.importJSON(fileId, collectionName, options);
- RMPCollection.importJSON(fileId, collectionName, options, dropCollection);
Parameters :
Name | Type | Description |
---|---|---|
fileId | String | Reference of an uploaded file |
collectionName | String | Name of the Mongo collection |
options | map | Options to be used during the call, all values are optional |
dropCollection (optional) | Boolean | if set to true, it creates the collection from scratch |
The "options" map can contain the following values:
Name | Type | Description |
---|---|---|
separator | String | Separator between two values (e.g. ; or , ). Default: , |
delimiter | String | Used to allow values containing a separator character (e.g. " or ' ),to include a delimiter in a value, use two delimiters (e.g. "" ). Default: " |
empty | String | Value to be used to represent an empty value (two separators in a row). Default: "" |
charset | String | Character set to be used to read the file content. Default: UTF-8 |
parse_numbers | Array | Names/indices of columns containing numerical values to be stored as Double. Example: ["age",3] will parse columns header "age" and the 4th column |
parse_longs | Array | Names/indices of columns containing long values to be stored as Long. Example: ["age",3] will parse column header "age" and the 4th column. |
trim | boolean | Trim values before inserting them in the collection. Trimming also occurs before decoding numerical values. |
Returns : The number of imported objects.
Type : Integer
Call Example :
<@script env="js">
var options = {};
var noOfRecords = RMPCollection.importJSON("fd58e98b-447e-42f8-8a78-24b55d928f3e", "property", options);
RMPData.setOutput(noOfRecords);
</@script>
Please give details of the problem