Code Monkey home page Code Monkey logo

mongodblink's Introduction

MongoDBLink

MongoDB driver for Mathematica

A fast client for MongoDB, built on the official MongoDB java driver.

Quick setup

This will install the package in the $UserAddOnsDirectory. You may prefer $AddOnsDirectory instead.

tmp = URLSave["https://github.com/zbjornson/MongoDBLink/archive/master.zip"];
dest = FileNameJoin[{$AddOnsDirectory, "Applications"}];
ExtractArchive[tmp, dest];
RenameDirectory[FileNameJoin[{dest, "MongoDBLink-master"}], FileNameJoin[{dest, "MongoDBLink"}]];
DeleteFile[tmp];
Print["Installed MongoDBLink to " <> dest]

Usage

New docs at http://zbjornson.github.io/MongoDBLink. These are incomplete and there are some interactivity bugs. When the docs are complete I will also distribute them with this package.

All symbols have usage text accessible with ?symbol, and Options[symbol]. Quick tutorial:

(* Load the package *)
<< MongoDBLink`

(* Connect to server. The server must already be running. *)
conn = OpenConnection[]; (* OpenConnection["host", port] and OpenConnection["host", port, "username", "password", "database"] also supported. *)

(* List databases. *)
DatabaseNames[conn]

(* Get a database *)
db = GetDatabase[conn, "name"]

(* List collections *)
CollectionNames[db]

(* Get a collection *)
coll = GetCollection[db, "name"]

(* Add some example data. The return value is the number of documents modified, which is 0 for an insert. *)
InsertDocument[coll, {"a" -> #, "b" -> 2 #}] &/@ Range[5]

(* Fetch all documents *)
FindDocuments[coll]

(* Limit the fields returned *)
FindDocuments[coll, "Fields" -> {"b"}]

(* Paginate results *)
FindDocuments[coll, "Offset"->1, "Limit"->1]

(* Use query operators. Refer to the MongoDB docs for info on valid operators. *)
FindDocuments[coll, {"a" -> {"$gt" -> 2}}]

(* Some Wolfram Query operators are transparently executed on the database server: *)
coll[Total, "a"]
coll[Min, "a"]
coll[Max, "a"]
coll[Mean, "a"]

coll[;;3]
coll[2;;3, {"a"}]

(* Any operator will work, but any other than those listed above effectively fetch ALL data and convert it to a Dataset for calculation. *)

(* Find distinct values of a field *)
FindDistinct[coll, "b"]
FindDistinct[coll, "b", {"a" -> {"$gt" -> {2}}}]

(* Count documents *)
CountDocuments[coll]
CountDocuments[coll, {"b" -> 2}]

(* Delete documents *)
DeleteDocument[coll, {"a" -> 3}]

(* Update or "upsert" documents. Note that without $set, the entire document will be replaced with the new document. *)
UpdateDocument[coll, {"a" -> 4}, {"$set" -> {"a" -> -4}}]

(* Create an ObjectId. When making queries against _id fields, values are automatically converted to ObjectIds. Use this when the key name is something other than _id. *)
oid = ObjectId[] (* random ObjectId *)
(* oid = ObjectId["hexstring"] from a valid hex string, which this is not *)

(* Cleanup stuff *)
DropCollection[coll];
DropDatabase[db] (* or DropDatabase[conn, "name"] *)
CloseConnection[conn];

Developer's guide

Most changes will only require modification of Mathematica code. If you need to modify java code, the build command is:

// in Java/src/
javac -d .. -classpath ../mongo-java-driver-3.0.4.jar:/path/to/SystemFiles/Links/JLink/JLink.jar MongoDBLinkUtils.java

replacing : with ; on Windows.

(I'm open for suggestion to improve this. To me this is less complicated than dealing with an Eclipse or NetBeans project for a project this small.)

Tests live in Tests.wlt and are runnable with

<< MongoDBLink`
TestReport["Tests.wlt"]

mongodblink's People

Contributors

zbjornson avatar rodrigomurta avatar

Watchers

James Cloos avatar ndroock1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.