MongoDB¶
MongoDB is a document database which is supported in this toolkit by a new port type and nodes.
A document database in contrast with a SQL database deals with more unstructured or semi-structured data and does not require a fixed schema to be defined in advance. This is an advantage when the domain is not yet well known or understood or when the data model is inherently unstructured.
It stores and operates on JSON-like documents and its data format can be regarded as an extension to JSON which can be translated back and forth to JSON.
MongoDB data format¶
When specifying data for MongoDB, there are some differences between how the data should be formatted in Sympathy compared with examples from MongoDB’s online documentation, for example, https://docs.mongodb.com/manual/crud/, or with how you would enter operations into the mongo shell. Here are the differences:
First of all, the MongoDB data used in Sympathy node configuration GUIs are actually in Python format but limited to literals and a selection of constructors. The data on the JSON data ports is encoded in JSON format. Port JSON data can be converted to literal Python format (for use in configuration GUIs): by renaming true, false to True, False respectively and by replacing null by None.
Then, all fields and operations are quoted when entered in Sympathy to be Python literals. Therefore, { age: { $gt: 18 } } in MongoDB shell format corresponds to { “age”: { “$gt”:18 } } in the format used by Sympathy.
Additionally, MongoDB (and BSON https://www.mongodb.com/basics/bson) supports other data types than what can be stored naturally in JSON or Python literals. To support these types in Sympathy, they are normally encoded as dictionaries with a single, special key. The following table gives two examples of how Sympathy does the encoding.
MongoDB shell format |
Sympathy Python literal |
---|---|
|
|
|
|
See https://docs.mongodb.com/manual/reference/bson-types/ for more details about types available in BSON.
Constructor functions¶
These helper constructors from pymongo (bson) can be used to express some data types that are not naturally expressed in literal Python, instead of using their corresponding encoding:
Binary(b”bytes”)
DBRef(“collection-name”, ObjectId(“62cff09ed08b15fac7f618c2”)),
Decimal128(“1.1”),
Int64(64)
MaxKey(),
MinKey(),
ObjectId(“62cff09ed08b15fac7f618c2”)
Regex(“^.$”)
Timestamp(100, 1)
See https://pymongo.readthedocs.io/en/stable/api/bson/ for more information, especially the submodules.
These two functions are also available for constructing datetimes:
ISODate(“2019-10-11”)
datetime(2019, 10, 11)
Output write operation¶
Several MongoDB nodes can output a write operation instead of performing their normal operation when the write operation port (opt-in) is created. In these cases, the node will output a specification instead of applying the operation directly. The operation can be used as part of the input list for a Bulk write operation.
Supported Mongo Server Versions¶
Sympathy supports Mongo Server versions 3.6, 4.0, 4.2, 4.4, 5.0, 6.0, 7.0.