5
$\begingroup$

I am connecting to a mongo database from Mathematica. I can get a MongoCollection object, but can not get a column of data from it.

tradesDB = MongoGetCollection[DB, "trades"];
barsDB = MongoGetCollection[DB, "bars"]

MongoCollectionFindOne[tradesDB]

<|"_id" -> MongoLink`BSONObjectID[ "5b57921b82737300c2909414"], "exch" -> "bina", "symb" -> "btc/usd", "id" -> 58789330, "time" -> 1.53247*10^12, "size" -> "0.26930800", "t" -> DateObject[{2018, 7, 24, 15, 54, 16.807}, "Instant", "Gregorian", -5., {"DateTime", ":", "Millisecond"}]|>

The above code is all successful, then I am trying to retrieve a column of data like:

tradesDB[All, {"t"}]    (*Code not working*)

In the end, I would like to have a time series plot of "t" and "size" from the above mongo collection.

Thanks for reading and any help is appreciated.

$\endgroup$

1 Answer 1

6
$\begingroup$

I've written this solution as an aggregation because aggregations are likely to be what you want to do more often, so will show you how to set it up.

disclaimer: I don't have your data so I am doing this without testing.

tradesDB = MongoGetCollection[db, "trades"]

create an aggregation in which you project the two fields that you are wanting

aggregate = MongoCollectionAggregate[tradesDB, {
   <|"$project" -> <|"_id" -> 0, "t" -> 1, "size" -> 1|>|>}]

This will return a cursor object (note that MongoCollectionFind[tradesDB,<||>, <|"_id" -> 0, "t" -> 1, "size" -> 1|>] will do this also but I wanted to illustrate aggregations)

To return some data to check what you have you can do something like this:

cursor = {};
Do[cursor = Flatten[{cursor, MongoCursorNext[aggregate]}], 5]
cursor

this should return a list of associations. You can retrieve all of the data (from the cursor point) straight into a Dataset

Dataset[aggregate]

If the above works for you perhaps you can post some screen grabs so that future readers can see the output. If it doesn't work ping me and maybe email your data for me to try.

Edit

To show this working I've implemented the above with a dummy set of data.

enter image description here

and here it is as a Dataset

enter image description here

In the next line I've done an aggregation to find the average population of the cities in the database with populations greater than 25000 from CA or NY to give an idea of building an aggregation using Mathematica and MongoLink. Hopefully it illustrates the power of using mongo from within Mathematica.

enter image description here

Edit 2

By the way if you wanted to use MongoCollectionFind the syntax is MongoCollectionFind[collection,query,projection]. If you simply want to project and not query then by analogy with mongo shell where you enter {} for the query, enter an empty association as above.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks very much!!! $\endgroup$ Oct 8, 2018 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.