[Beginner] How to use MongoDB functions

Basic MongoDB tasks:
MONGO.STORE(); MONGO.QUERY(); MONGO.DELETE(); MONGO.COUNT(); MONGO.REPLACE()

Using a database can be very convenient. A Streamsheet is good at processing data, but when it comes to storing a huge amount of data a database comes in handy. One of the many alternatives is the MongoDB. You can connect a MongoDB via a MongoDB Connector. If you don´t already have a MongoDB check out this very simple tutorial. https://docs.cedalo.com/tutorials/addons.html#mongodb

Open the “Function Wizard” to gain convenient access to the MongoDB functions.

MONGO.STORE():

=MONGO.STORE(“Stream”,“Collection”,“Document”)
image

As soon as your MongoDB Connector and Producer is set up you can start storing data.
Click on a cell, open the function wizard and select MONGO.STORE. Remember the calculation order of a Streamsheet (top to bottom, left to right), this might play a role in the structure of your logic.

Stream:
The “Function Wizard” automatically selects a stream. Make sure the right one is selected.

Collection:
MongoDB has different places to store data at. These are called collections. Type in a name of a collection. The collection will automatically be created, if not already existing.

Document:
The document contains your data. It is a simple cell range of key and value pairs. It is also possible to order them in a hierarchy.

With every calculation one document will be stored in the selected collection.

MONGO.QUERY():
=MONGO.QUERY(“Stream”,“Collection”,“Query”,“Target”,“ResultKeys”,“PageSize”,“Page”,“Sort”,“Timeout”)
image

To lookup data from a MongoDB collection use the function MONGO.QUERY. It is possible to access either all stored documents or a subset of them.

Stream:
Again select the right Producer to connect to the right database.

Collection:
Type in the collection you used in the MONGO.STORE function.

Query:
The query filters the data and only gives back documents, which are match or are composed of the query. Define one or more key value pairs the documents you are searching for have to contain. E.g. you have saved a lot of customer information and now need the information for all customer with the name “Julia”, define a horizontal cell range containing “name” on the left and “Julia” on the right.
To access all data just leave this section empty.

Target:
Define where the response of the database should be displayed. Choose a cell range or =INBOX(); =OUTBOX() as a target. Be careful, the cell range maybe to small to display all information.

Result Keys:
The Query always gives back all the information saved in a document. If you are only interested in a subset write each key in a cell and enter the cell/ cell range in the Result Key field.

Page Size:
The Page Size defines how many results will be shown in the response of your query per page. As a default, page 1 will be returned.

Page:
Select the page you want to get back. Example: Enter page size = 5 and page = 2. The query will return entry 6-10.

Sort:
You can either sort by time using 1 and -1 or you can sort alphabetically by using a cell range with the key to sort with and a 1 or -1. 1 = descending; -1 = ascending.

Timeout:
Tells the function how long to wait for a response from the database(in ms).

MONGO.DELETE():
=MONGO.DELETE(“Stream”,“Collection”,“Query”,“Target”,“Timeout”)
image

Delete documents in a collection.

Stream:
Select the Stream connected to your MongoDB.

Collection:
Enter the collection you want to adjust entries in.

Query:
All documents with the defined key value pair/s will be deleted from the collection.

Target:
MONGO.DELETE returns the amount of objects that have been deleted. To get this information define either a cell range of at least 2x2 or enter =INBOX()/=OUTBOX().

Timeout:
Tells the function how long to wait for a response from the database(in ms).

MONGO.COUNT():
=MONGO.COUNT(“Stream”,“Collection”,“Query”,“Target”,“Timeout”)
image

If you are interested in the amount of documents stored in a collection use MONGO.COUNT().

Stream:
Select the Stream connected to your MongoDB.

Collection:
Enter the collection you want to count entries in.

Query:
Enter a cell range of key value pairs. All documents with the defined key value pair/s will be counted.

Target:
MONGO.COUNT returns the amount of objects that have been counted. To get this information define either a cell range of at least 1x2 or enter =INBOX()/=OUTBOX().

Timeout:
Tells the function how long to wait for a response from the database(in ms).

MONGO.REPLACE():
=MONGO.REPLACE(“Stream”,“Collection”,“Query”,“Document”,“Upsert”)
image

To replace data in a collection, use the MONGO.REPLACE() function.

Stream:
Select the Stream connected to your MongoDB.

Collection:
Enter the collection you want to replace entries in.

Query:
The query searches for documents within a collection to replace.

Document:
Define a document to replace existing data with and enter the cell range here.

Upsert:
TRUE or FALSE. Upsert decides, if, when a query can´t be found, the document is added to the collection or dismissed.

If you are looking for the MONGO.AGGREGATE() tutorial click here.