MongoDB Query Language

Cosmocloud is natively integrated with MongoDB as the primary database and can run native MongoDB Queries.

Custom MongoDB queries can be written supported in these nodes -

To learn more about MongoDB Query Language, you can check the official documentation of MongoDB.

BSON to EJSON

MongoDB Extended JSON (EJSON) is a string format for representing BSON documents. This specification defines the canonical format for representing each BSON type in the Extended JSON format.

Cosmocloud supports MongoDB Queries converted in Extended JSON format. You need to convert some of the rich BSON features (such as ObjectID) to EJSON way to represent the same.

For example, you might have a sample aggregation query like this -

[
    {
        "$match": {
                "paymentId": ObjectId("123456789012345678901234")
        }
    }
]

As JSON does not support custom classes like ObjectId and similar, you need to convert the same into EJSON format as below -

[
    {
        "$match": {
                "paymentId": {
                        "$oid": "123456789012345678901234"
                }
        }
    }
]

As you can see, each special item in MongoDB query can be converted to EJSON structure, so that the system can easily read it and pass the same to MongoDB.

Conversion table

BSON TypeCanonical Extended JSON Format

ObjectId

{"$oid": <ObjectId bytes as 24-character, big-endian hex string>} Example -

Regular Expression

{"$regularExpression": {pattern: string, "options": <BSON regular expression options as a string>}} Example -

Datetime Object (Canonical form)

{"$date": {"$numberLong": <epoch in millisec, as a string>}} Example -

Datetime Object (Relaxed form)

{"$date": "ISO Datetime Format with maximum time precision of milliseconds, as a string"} Example -

Boolean

true or false

Null

null

For more references, you can check the official specification document.

Last updated