MongoDB

MongoDB is a database based on distributed file storage. The DolphinDB mongodb plugin can connect to the MongoDB server and import its data to DolphinDB in-memory tables. This plugin is developed based on the MongoDB C Driver (libmongoc).

Installation (with installPlugin)

Required server version: DolphinDB 2.00.10 or higher

OS: Windows, Windows JIT, Linux x86, and Linux JIT.

Installation Steps:

(1) Use listRemotePlugins to check plugin information in the plugin repository.

Note: For plugins not included in the provided list, you can install through precompiled binaries or compile from source. These files can be accessed from our GitHub repository by switching to the appropriate version branch.

login("admin", "123456")
listRemotePlugins()

(2) Invoke installPlugin for plugin installation

installPlugin("mongodb")

(3) Use loadPlugin to load the plugin before using the plugin methods.

loadPlugin("mongodb")

Method References

connect

Syntax

connect(host, port, user, password, [db])

Details

Build a connection with the MongoDB server. Return a connection handle that can be used when calling load and other methods.

Parameters

  • host: A STRING scalar indicating the address of the MongoDB server.
  • port: A STRING scalar indicating the port number of the MongoDB server.
  • user: A STRING scalar indicating the username to the MongoDB server. If the MongoDB authentication service is disabled, enter an empty string "".
  • password: A STRING scalar indicating the password to the MongoDB server. If the MongoDB authentication service is disabled, enter an empty string "".
  • db (optional): A STRING scalar indicating the MongoDB authentication database for the specified user, which contains the credential information on the user. If not specified, the "admin" database from the MongoDB server (specified by host) is used.

Examples

conn = mongodb::connect(`localhost, 27017, `root, `root, `DolphinDB)
conn2 = mongodb::connect(`localhost, 27017, `root, `root)

load

Syntax

load(connection, collcetionName, query, option, [schema])

Details

Import the query result in MongoDB into a DolphinDB in-memory table. For information on the supported data types, see the Data Type section in the DolphinDB User Manual.

Parameters

  • connection: The MongoDB connection handle returned by method connect.
  • collectionName: The name of a MongoDB collection. There are two options to specify this parameter:
    • Specify only the "collection name" - the system searches for the collection in the database (db) specified in connect.
    • Specify "databaseName:collectionName" - the system searches for the collection in the specified database.
  • query: A JSON string indicating the query conditions in MongoDB, such as { "aa" : { "$numberInt" : "13232" } }, { "datetime" : { "$gt" : {"$date":"2019-02-28T00:00:00.000Z" }} }.
  • option: A JSON string indicating the query options in MongoDB. For example: {"limit":123} limits the number of records or documents returned in the query.
  • schema (optional): A table containing column names and types. To modify the data type of a column that is automatically determined by the system, the schema table needs to be modified and passed as a parameter in load.

Examples

conn = mongodb::connect(`localhost, 27017, `root, `root, `DolphinDB)
query='{ "datetime" : { "$gt" : {"$date":"2019-02-28T00:00:00.000Z" }} }'
option='{"limit":1234}'
tb=mongodb::load(conn, `US,query,option)
select count(*) from tb
tb2 = mongodb::load(conn, 'dolphindb:US',query,option)
select count(*) from tb
schema=table(`item`type`qty as name,`STRING`STRING`INT as type)
tb2 = mongodb::load(conn, 'dolphindb:US',query,option,schema)

aggregate

Syntax

aggregate(connection, collcetionName, pipeline, option, [schema])

Details

Import the result of an aggregate operation in MongoDB into a DolphinDB in-memory table. For information on the supported data types, see the Data Type section in the DolphinDB User Manual.

Parameters

  • connection: The MongoDB connection handle returned by method connect.
  • collectionName: The name of a MongoDB collection. There are two options to specify this parameter:
    • Specify only the "collection name" - the system searches for the collection in the database (db) specified in connect.
    • Specify "databaseName:collectionName" - the system searches for the collection in the specified database.
  • pipeline: A JSON string indicating the MongoDB aggregation pipeline, such as {$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}.
  • option: A JSON string indicating the query options in MongoDB. For example: {"limit":123} limits the number of records or documents returned in the query.
  • schema (optional): A table containing column names and types. To modify the data type of a column that is automatically determined by the system, the schema table needs to be modified and passed as a parameter in aggregate.

Examples

conn = mongodb::connect(`localhost, 27017, "", "", `DolphinDB)
pipeline = "{ \"pipeline\" : [ { \"$project\" : { \"str\" : \"$obj1.str\" } } ] }"
option = "{}"
mongodb::aggregate(conn, "test1:collnetion1",pipeline,option)

close

Syntax

close(connection)

Details

Close a MongoDB server connection.

Parameters

  • connection: The MongoDB connection handle returned by method connect.

Examples

conn = mongodb::connect(`localhost, 27017, `root, `root, `DolphinDB)
query = `{ "datetime" : { "$gt" : {"$date":"2019-02-28T00:00:00.000Z" }} }
option = `{"limit":1234}
tb = mongodb::load(conn, `US,query,option)
select count(*) from tb
mongodb::close(conn)

parseJson

Syntax

parseJson(str, keys, colnames, colTypes)

Details

Parse a JSON string and convert it into a DolphinDB in-memory table. Return the in-memory table.

Parameters

  • str: A STRING vector indicating the JSON string to be converted.
  • keys: A STRING vector indicating the keys of str.
  • colnames: A STRING vector indicating the column names in the result table. The column names correspond to the keys of str.
  • colTypes: A vector indicating the column types in the result table. Supported DolphinDB data types: BOOL, INT, FLOAT, DOUBLE, STRING and array vector of BOOL, INT, and DOUBLE type. You can convert the INT, FLOAT, and DOUBLE types in JSON into any of the INT, FLOAT, or DOUBLE types in DolphinDB.

Example

data = ['{"a": 1, "b": 2}', '{"a": 2, "b": 3}']
 mongodb::parseJson(data, 
`a`b, 
`col1`col2,
[INT, INT] )

getCollections

Syntax

getCollections([databaseName])

Parameters

  • databaseName (optional): A string indicating a MongoDB database to be queried. If not specified, the database specified in method connect will be used.

Details

Get the names of all the collections in the specified database.

Examples

conn = mongodb::connect("192.168.1.38", 27017, "", "")
mongodb::getCollections(conn, "dolphindb")

Usage Examples

query = '{"dt": { "$date" : "2016-06-22T00:00:00.000Z" } }';
query = '{"bcol" : false }';
query = '{"open" : { "$numberInt" : "13232" } }';
query = '{"vol" : { "$numberLong" : "1242434"} }';
query = ' {"close" : { "$numberDouble" : "1.2199999999999999734" }';
query = '{"low" : { "$gt" : { "$numberDecimal" : "0.219711" } } }';
query = '{"uid" : { "$oid" : "1232430aa00000000000be0a" } }';
query = ' {"sid" : { "$symbol" : "fdf" } }';
query = '{"symbol" : "XRPUSDT.BNC"}';
query = '{"ts" : { "$date" : { "$numberLong" : "1600166651000" } }';
query = '{}';
option = '{}';
con = mongodb::connect(`localhost,27017,`admin,`123456,`dolphindb);
res = mongodb::load(con,`collection1,query,option);
mongodb::close(con);
t = select * from res

Data Type Mappings

Integral

Data Type in MongoDBData Type in DolphinDB
int32INT
int64(long)LONG
boolBOOL

In DolphinDB, the smallest value of each integral type (e.g. -2,147,483,648 for INT and -9,223,372,036,854,775,808 for LONG) is a NULL value.

Float

Data Type in MongoDBData Type in DolphinDB
doubleDOUBLE
decimal128DOUBLE

Temporal

Data Type in MongoDBData Type in DolphinDB
doubleDOUBLE
decimal128DOUBLE

Literal

Data Type in MongoDBData Type in DolphinDB
stringSTRING
symbolSTRING
oidSTRING