9
$\begingroup$

I try connect mathematica with my database NOSQL mongoDB but I have problem with create collection. I have Mathematica 10.2 and I download driver .net version 2.2 from this website a link! I unpacking driver to :

    input : FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "NETLink"}]
output : "C:\\Mathematica\\SystemFiles\\Links\\NETLink"

After unpacking zip folder to "C:\Mathematica\SystemFiles\Links\NETLink" I install this pack and I connect to database and try createCollection but i have problem this is my code with output:

    In[2]:= InstallNET[] 
    Out[2]= LinkObject["C:\\Mathematica\\SystemFiles\\Links\\NETLink\\\InstallableNET.exe", 127, 4]
    In[13]:= conn = ConnectToMongoDB["localhost"]
    Out[13]= NETLink`Objects`NETObject$377462395502593
    In[5]:= GetMongoServer[conn_] := Module[{}, conn@GetServer[]]
    In[6]:= createCollection[conn_, db_String, collectionname_String] := Module[{server, database},
    server = GetMongoServer[conn];
    database = server@GetDatabase[db];
    database@CreateCollection[collectionname]
    ]
    In[8]:= createCollection[conn, "mydb", "test"]
    During evaluation of In[8]:=
    NET::nomethod: No public instance method named GetServer exists for the .NET type MongoDB.Driver.MongoClient.
    Out[8]= $Failed[GetDatabase["mydb"]][CreateCollection["test"]]

I use MongoDB on Windows and I switch to mydb

    > use mydb
switched to db mydb

I have no idea what I'm doing wrong maybe some suggestions?


I must use only mathematica because I write engineering work about database connections and I must to use java or .Net. Thank you for this link b.gatessucks but when I use database link in java I have problem this problem:

    In[1]:= << MongoDBLink`
In[4]:= conn = OpenConnection[];
In[5]:= DatabaseNames[conn]
Out[5]= {"local"}
In[18]:= db = GetDatabase[conn, "local"]
In[19]:= CollectionNames[db]
Out[19]= {"name", "startup_log"}
In[20]:= coll = GetCollection[db, "name"]
In[21]:= InsertDocument[coll, {"a" -> #, "b" -> 2 #}] & /@ Range[5]
Out[21]= {0, 0, 0, 0, 0}
In[23]:= FindDocuments[coll]


  During evaluation of In[23]:= Java::excptn: A Java exception occurred: java.lang.UnsupportedClassVersionError: MongoDBLinkUtils : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source).
During evaluation of In[23]:= LoadJavaClass::fail: Java failed to load class MongoDBLinkUtils. >>
Out[23]= MongoDBLinkUtils`Iterate[JLink`Objects`vm1`\
JavaObject11819681379778561]
$\endgroup$
11
  • $\begingroup$ Maybe useful link. $\endgroup$ Commented Jan 12, 2016 at 22:59
  • $\begingroup$ the problem doesn't appear to be creating the collection but the function GetServer[]. Have you read the driver docs? $\endgroup$ Commented Jan 12, 2016 at 23:04
  • $\begingroup$ yes and i watching youtube.com/watch?v=oU3y-kiFYLs this video and she had used this function. We can see this in 5:23 min. $\endgroup$
    – bartek
    Commented Jan 12, 2016 at 23:11
  • $\begingroup$ okay next question then is whether there are some driver dependencies. Also do you need to use .NET? $\endgroup$ Commented Jan 13, 2016 at 0:47
  • 1
    $\begingroup$ @ZachB I just tried your latest download and had no problems. I already had Java 1.8.0_66 installed so not sure why compatibility with earlier versions was required for me but in any case it works now. Thanks! $\endgroup$ Commented Jan 18, 2016 at 3:57

2 Answers 2

10
$\begingroup$

Wolfram Language version 11.3 now has support for MongoDB.

You can connect to a MongoDB database via MongoConnect. For example, connecting to a database running locally:

In[1]:= Needs["MongoLink`"]
client = MongoConnect[];
MongoGetDatabaseNames[client]

Out[2]= {"Test", "admin"}
$\endgroup$
3
$\begingroup$

If you want to bypass .NET and do something that I personally find more intuitive then run it all through the terminal. I'm running OS X 10.10 and have Mongo 3.2 installed. Create a document:

json = ExportString[{"old mcdonald" -> {"pig" -> 2, "cow" -> 4}}, "JSON"]

"{
    \"old mcdonald\": {
        \"pig\": 2,
        \"cow\": 4
    }
}"

Then add that document to your collection. Note that you do not have to create the collection ("farm") to begin with, it is automatically created. Ditto the database, called "example" in the code below:

ReadList["!/usr/local/mongodb/bin/mongo example --eval 'db.farm.insert(" <> json <> ")'", String]

(* {"MongoDB shell version: 3.2.0", "connecting to: example", "WriteResult({ \"nInserted\" : 1 })"} *)

Now go to your Mongo shell and verify the new collection:

enter image description here

I find this far less clunky than using the Java driver and associated functions (same applies to .NET)

$\endgroup$

Your Answer

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

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