Skip to content

MongoDB

The documentation applies to:✅ v0.8.0

Preparation knowledge

We recommend you to read carefully some sections below before you read more steps on this document:

  • Page concept link
  • State Management link
  • Global variables link

Aggregation Pipeline

We choose Aggregation MongoDB for most of queries on LET Portal. However, LET Portal will provide some customs which help you to reduce a complexity of BsonDocument.

LET Portal is providing a platform for helping reduce a complexity and time. Basically, we will provide many examples for guiding you to create Dynamic List, Standard, Chart instead of wring a lot of codes. So you don't want to worry about Aggregation Pipeline. Only thing you want to learn deep-dive on MongoDB when you have a complexity query.

Recommendation

Due to MongoDB BsonDocument, many developers got confusion on learning Aggregation Pipeline. So we strongly recommend you to install MongoDB Compass to play around with MongoDB before you execute. When you have enough knowledge of Aggregation Pipeline, then let's explore this documentation

BsonDocument - Problems on LET Portal

BsonDocument looks like JsonDocument but they have some differents. One of most different is Bson supports a function without " and Json doesn't. Unfortunately, LET Portal only has Json Editor (an open-source project) for inputting Bson.

Due to a problem ", we help you to eliminate " when executing on MongoDB. There are some common functions which will be eliminated ":

  • ObjectId
  • ISODate
  • NumberLong

``` json tab="Json" { "id": "ObjectId('{{data.id}}')" }

``` bson tab="Bson"
{
    "id": ObjectId('{{data.id}}')
}

Interpolation {{}}

For binding data from Global Variables, you need to use {{ }} on your Json.

By default, you can bind data as string, except Object type.

For example with basic type

``` json tab="basic type" { "id": "ObjectId('{{data.id}}')" }

``` bson tab="translate"
{
    "id": ObjectId('abc')
}

For example with Object type

``` json tab="Object type" { "user": "{{data}}" }

``` bson tab="Translate"
{
    "user": {
        "id": "abc",
        "name": "xyz"
    }
}

Command Types

As we said, LET Portal provide a custom Json which helps you to work easier with MongoDB. Let's check the screenshot below

Custom json for MongoDb

(1): This is a command types, we are four types: $query, $insert, $update, $delete

(2): This is an effective collection name on chosen Database

Result format

All executions have a same json format is

{
    "result": { }, //Dynamic object
    "isSuccess": "true",
    "error": "Some error codes"
}

However, you don't care too much about this result format because LET Portal will wrap it for you. If isSuccess=true, we will detach result object as data

$query

First command type is $query, let's take a look some tabs below:

``` json tab="Pattern" { "$query":{ "collection_name": [ // Aggregation Pipeline ] } }

``` json tab="Example"
{
    "$query": {
        "apps": [
            {
                "$match": {
                    "_id": "ObjectId('{{data.id}}')"
                }
            }
        ]
    }
}

``` bson tab="Translated" { "query": { "apps": [ { "match": { "_id": ObjectId('{{data.id}}') } } ] } }

As you see, an array of `collection_name` is Aggregation Pipeline of MongoDB. LET Portal will detach this array to execute as Aggregation Pipeline.

## $union

If you have an experience with MongoDB, Aggregation Pipeline doesn't have Union feature yet. So that's why we create this keyword to make your better life.

``` json tab="Pattern"
{
    "$query": {
        "$union": [
            {
                "collection_name1": []
            },
            {
                "collection_name1": []
            }
        ]
    }
}

Basically, $union helps you to merge two same results into one.

$insert

``` json tab="Pattern" { "insert":{ "collection_name": { "data": { // Json for inserting Bson document }, // Some static fields, useful for non-control field such as createdDate } } }

``` json tab="Example"
{
    "$insert": {
        "apps": {
            "$data": "{{data}}",
            "author": "{{user.username}}",
            "createdDate": "ISODate('{{currentISODate()}}')",
            "updatedDate": "ISODate('{{currentISODate()}}')"
        }
    }
}

``` bson tab="Translated" { "insert": { "apps": { "data": { "_id": "ObjectId('abc')", "name": "Core" }, "author": "admin", "createdDate": "ISODate('2020-02-02 00:00')", "updatedDate": "ISODate('2020-02-02 00:00')" } } }

## $update

``` json tab="Pattern"
{
    "$update":{
        "collection_name": {
            "$data": {
                // Json for updating Bson document
            },
            // Some static field, useful for non-control field such as modifiedDate
            "$where": {
                // Condition for finding updating document
            }
        }
    }
}

``` json tab="Example" { "update": { "apps": { "data": "{{data}}", "updatedDate": "ISODate('{{currentISODate()}}')", "$where": { "_id": "ObjectId('{{data.id}}')" } } } }

``` bson tab="Translated"
{
    "$update": {
        "apps": {
            "$data": {
                "_id": "ObjectId('abc')",
                "name": "Core"
            },
            "updatedDate": "ISODate('2020-02-02 00:00')",
            "$where": {
                "_id": "ObjectId('abc')"
            }
        }
    }
}

$delete

``` json tab="Pattern" { "delete":{ "collection_name": { "where": { // Condition for finding deleting documents } } } }

``` json tab="Example"
{
  "$delete":{
    "apps": {
      "$where": {
        "_id": "ObjectId('{{data.id}}')"
      }
    }
  }
}

bson tab="Translated" { "$delete":{ "apps": { "$where": { "_id": "ObjectId('abc')" } } } }