MongoDB Collections
1. Introduction to MongoDB in SoftyflowDirect link to 1. Introduction to MongoDB in Softyflow
In the Softyflow ecosystem, Collections are built upon MongoDB, a powerful, cross-platform, document-oriented database. MongoDB is renowned for its high performance, high availability, and easy scalability, making it an ideal choice for managing your application's data. It operates on the concept of collections and documents, which offers a flexible and scalable alternative to traditional relational databases (RDBMS).

1.1. Understanding the Core ConceptsDirect link to 1.1. Understanding the Core Concepts
To effectively use Collections in Softyflow, it's important to grasp the fundamental structure of MongoDB.
1.1.1. DatabasesDirect link to 1.1.1. Databases
A Database acts as a physical container for your collections. Each database has its own set of files on the file system, and a single MongoDB server can manage multiple databases.
1.1.2. CollectionsDirect link to 1.1.2. Collections
A Collection is a grouping of MongoDB documents, analogous to a table in a relational database. It exists within a single database and does not enforce a rigid schema. This means documents within the same collection can have different fields, though they typically serve a similar or related purpose.
1.1.3. DocumentsDirect link to 1.1.3. Documents
A Document is a set of key-value pairs where all the data is stored. Documents feature a dynamic schema, which allows for significant flexibility. Documents in the same collection don't need to share the same structure, and common fields can hold different types of data.
1.1.3.1. Sample Document StructureDirect link to 1.1.3.1. Sample Document Structure
Here is an example of what a MongoDB document looks like. This structure allows for complex and nested data.
{
"_id": "ObjectId(\"7df78ad8902c\")",
"title": "MongoDB Overview",
"description": "MongoDB is a NoSQL database",
"by": "Tutorials Point",
"tags": ["mongodb", "database", "NoSQL"],
"likes": 100,
"comments": [
{
"user": "user1",
"message": "My first comment",
"dateCreated": "new Date(\"2011-01-20T02:15:00Z\")",
"like": 0
},
{
"user": "user2",
"message": "My second comment",
"dateCreated": "new Date(\"2011-01-25T07:45:00Z\")",
"like": 5
}
]
}
The _id field is a 12-byte hexadecimal number that ensures every document is unique. You can provide your own _id when inserting a document. If you don't, MongoDB will automatically generate a unique ID.
1.2. External MongoDB ResourcesDirect link to 1.2. External MongoDB Resources
For more in-depth information about MongoDB's capabilities, you can refer to the official documentation:
- Query and Projection Operators: These operators allow you to locate data and modify how it is presented.
- Update Operators: These operators enable you to modify data or add new data to your database.
- Aggregation Pipeline Stages: Learn about the stages available in the Aggregation Pipeline.
- Aggregation Pipeline Operators: Explore the operators used to define and manipulate documents in pipeline stages.
2. Working with Collections in SoftyflowDirect link to 2. Working with Collections in Softyflow
Softyflow simplifies the process of creating and managing MongoDB collections directly from the studio. Each collection keeps its documents separately in the three environments, TEST, UAT and PROD: the data of your tests never mixes with the production data.
2.1. Creating a New CollectionDirect link to 2.1. Creating a New Collection
You can create a collection from its project or from the workspace.
In a project (Build mode):
- Click Collections in the dock of the project. The list of the project's collections opens.
- Click New collection at the bottom of the list. The Collection card and the + of the Collections block of the project's Build home open the same card.
- Type the Name of the collection. Spaces become
_: the card shows the final name (e.g.customer ordersbecomescustomer_orders). - Click Create. The collection opens in a new tab of the project.

From the workspace: open Collections in the dock (shortcut G then C; the page requires the Databases right) and click Create Collection. The Create new collection dialog asks for the Collection name and the Project of the collection; click Create. Spaces become _ here too. The new collection appears in the list: click it to open its page.
2.2. The Page of a CollectionDirect link to 2.2. The Page of a Collection
A click on a collection (in the project dock, on the Build home of the project or in the workspace Collections list) opens its page. A palette of tiles on the left gives its views, Data (Overview and Documents) and Settings (Rights and Usages), above a summary of the number of documents in each environment.

Overview: one card per environment (TEST, UAT, PROD) with the number of Documents, the Size, and five actions:
- Open as JSON opens the 100 most recent documents of the environment as raw JSON in a new tab;
- Download saves all the documents of the environment in a JSON file (
<name>_<environment>.json); - Import loads a JSON file (an array of documents, like the file of Download) into the environment. First choose what to do When a document already has the same _id: Copy with a new _id (keeps the existing document and adds a copy), Overwrite (replaces the existing document), Skip (keeps the existing document) or Always a new _id (every document is inserted, even without conflict). Then drop the file, or click to choose it;
- Migrate copies the documents of the environment to another environment (To), with the same choice for the documents that already exist;
- Clear permanently deletes the documents of the environment, after a confirmation.
Documents: the documents of the selected environment (TEST, UAT or PROD), page by page, shown as a table. Click a row to see the whole document. A cell that holds an object or a list shows its content when you click it, and Copy the JSON copies that field. Raw JSON opens the 100 most recent documents of the environment as raw JSON in a new tab; to get all of them, use Download in the Overview.

The Documents view of a collection, in TEST. Rights: each role gets its Create, Read, Update and Delete rights on the data of the collection, in every environment (Add a role). With no role, nobody can use the data of the collection. Save the rights with the save button of the rail on the right, or Ctrl + S.
Usages: choose the Resources version and click Search to find the interfaces, processes and APIs of every project that use the collection.

See also Database Management › MongoDB Collections.
2.3. Performing CRUD OperationsDirect link to 2.3. Performing CRUD Operations
CRUD (Create, Read, Update, Delete) operations in MongoDB are performed on a single collection at a time. Softyflow provides actions of the Collection category and SDK methods to handle these operations.
From a page (actions, widgets, page SDK), every operation is checked against the rights of the user's roles on the collection (Rights view, see 2.2): Create to insert, Read to find, count and aggregate, Update to update, Delete to delete. An upsert (the massUpsertCollection action, massUpsert, updateMany with the upsert option) requires Update, and only creates documents if the user also has Create (see 2.3.3).
2.3.1. Create (Insert) OperationsDirect link to 2.3.1. Create (Insert) Operations
Insert operations in MongoDB are atomic at the single-document level. Softyflow supports the standard MongoDB insert methods.
- InsertOne in Softyflow: the addToCollection action (Insert new data into collection) inserts a single document into your collection from a page or a process. In a page, it saves the data of a form (Attach form) or a data model that you write. For processes, you first need to create an input/output variable and then select the appropriate action. More details can be found in the Actions documentation.
Example: Submitting a Form to a Collection
The following image shows a multi-step action triggered by a button. First, validateForm checks the data of the Main Form. Then, addToCollection saves the form data as a new document in demo_collection (the ID of the new document goes to the insertedId variable). Finally, refreshWidget reloads the table of the page to display the new entry.

- InsertMany in Softyflow: in a page, the addManyToCollection action (Insert many data into collection) inserts several documents at once. In a script, use
insertManyof the Collection class of the Web Modeler SDK, orSF.collection.insertManyin a process script.
2.3.2. Read (Find) OperationsDirect link to 2.3.2. Read (Find) Operations
Read operations allow you to retrieve documents using specific query filters. You can learn more about query filters from the MongoDB documentation.
Find in Softyflow: the getCollection action (Retrieve a list of data from collection) returns the documents that match its Criteria (a MongoDB query filter) into the variable named in Dump var. It is available in pages, processes and APIs. In a page, it returns at most 100 documents, the most recent first; in processes and APIs, it returns all the matching documents. To read more documents from a page, use
find({ criteria, limit, page })of the Web Modeler SDK (limit: 0for no limit), or the AggregateCollection action, which has no default limit (add a$limitstage to cap it). A table or a dropdown can also read a collection directly through its data source (see Usage in Widgets).FindOne in Softyflow: the getDocument action (Retrieve one document from collection) reads one document by its Document id. In a page, it fills a form (Attach form) or a variable; in a process, the document is the value returned by the action.
Aggregate in Softyflow: the AggregateCollection action (Mongo Aggregation Query) runs a full aggregation pipeline on the collection. See MongoDB Aggregation.
Example: Loading Data into a Form
This image shows a getDocument action that reads, in demo_collection, the document whose ID is in the document_id variable, and uses its data to populate the fields of the Main Form.

2.3.3. Update OperationsDirect link to 2.3.3. Update Operations
Update operations modify existing documents in a collection and are atomic at the document level. Be cautious, as updates are permanent.
- UpdateOne in Softyflow: the updateCollection action (Update data collection) updates one document, given by its Document id, with the data of a form or of a data model. It is available in pages, processes and APIs.
Example: Updating a Document from a Form
The image below illustrates an updateCollection action that updates a document of demo_collection by referencing its unique _id, with the data of the Main Form.

UpdateMany in Softyflow: in a page, the massUpsertCollection action (Mass upsert data collection) saves an array of documents at once and requires the Update right: a row with an
_idupdates that document. A row without_idcreates a new document only if the user also has the Create right on the collection. Without it, the whole action is refused ("creating documents requires the create right") and nothing is written: only rows with an_idare accepted. In a script, useupdateManyormassUpsertof the Web Modeler SDK, orSF.collection.updatein a process script. From a page,updateManywith theupsertoption follows the same rule: without the Create right, it only updates the existing documents, and it is refused when nothing matches. See What Softyflow Enforces.ReplaceOne: This operation replaces an entire document, except for the
_idfield.
2.3.4. Delete OperationsDirect link to 2.3.4. Delete Operations
Delete operations are used to remove documents from a collection and are also atomic at the document level. Like updates, deletions are permanent.
DeleteOne in Softyflow: the deleteDocument action (Remove one document from collection) deletes one document, given by its Document id, from a page, a process or an API.
DeleteMany in Softyflow: use a script. In a process script,
SF.collection.deletedeletes every document that matches a query. Thedeletemethod of the Web Modeler SDK deletes one document by its ID. To empty an environment of the collection, use Clear on its page (see 2.2).
2.4. Limits of the Queries Sent from a PageDirect link to 2.4. Limits of the Queries Sent from a Page
Since 8.6, the server checks the criteria and the aggregation pipelines that a page sends to a collection (the data source of a table or a dropdown, the getCollection and AggregateCollection actions, the find, count and aggregate methods of the page SDK) before they reach MongoDB:
- Only read stages are accepted (
$match,$project,$group,$sort,$lookup,$facet…).$outand$merge(they write into the database), and$function,$accumulatorand$where(they run JavaScript on the database server), are refused at any depth, sub-pipelines and expressions included, as is any stage that is not a read stage. The request then fails with a400error "Aggregation refused: …". $lookup,$graphLookupand$unionWithmay only read the collection itself, or another Softyflow collection that the user is allowed to read with their roles in that environment. The collection of the users is never a target.- An
_idgiven as a 24-character hexadecimal string in a criteria, or in the first$matchof a pipeline, also matches the ObjectId: pages keep document IDs as strings, and the criteria still find the document. - The process scripts (
SF.collection, on the server) are not concerned: they read the database directly.
The pipelines and filters of the existing pages, reports and actions use none of the refused stages and keep working. See also What Softyflow Enforces.