Connecting and getting data from a local MongoDB database

Creating desktop or client-server database solutions?

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
javmirval
Posts: 9
Joined: Sun Feb 09, 2014 5:55 am

Connecting and getting data from a local MongoDB database

Post by javmirval » Sun Feb 09, 2014 6:30 am

Hi friends, can you give me a hand on how to connect to a Mongo database and how to read from it from within Rev? In a recent post I made, I asked something about revdb, in the understanding that it is being revised to support NoSQL databases. Incredibly enough, every day, while I learn a little bit more about this NoSQL thing, I tend to think less and less in terms of "Joins" :D Regards, Javier

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5132
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Connecting and getting data from a local MongoDB databas

Post by Mark » Sun Feb 09, 2014 9:16 pm

Hi Javier,

"Support for NoSQL databases" isn't something you can add by making one big change in the LiveCode engine. Every database engine has its own way to provide its service and for each database system, a special driver or external needs to be created, unless the database uses a standard protocol like ODBC.

While such a driver or external is unavailable, you can probably use the shell function to communicate with the database. Although typing

Code: Select all

mongo
in a terminal or command prompt would start mongo in a shell, but if you add the --eval parameter, you can execute the commands immediately, without entering the shell first:

Code: Select all

mongo --eval "printjson(db.serverStatus())"
Additionally, you can write a script and save it in a .js file, which is then executed with

Code: Select all

mongo yourFile.js
or

Code: Select all

mongo < yourFile.js
You'll have to read the mongo documentation if you want to know what else the mongo command line utility can do.

To execute mongo shell commands in LiveCode, use the shell function, as in

Code: Select all

put shell("mongo --eval" && quote & "printjson(db.serverStatus())" & quote)
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

MaxV
Posts: 1574
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Connecting and getting data from a local MongoDB databas

Post by MaxV » Mon Feb 10, 2014 2:21 pm

Probably you can use the ODBC driver for MongoDB: http://blog.mongodb.org/post/4985203651 ... or-mongodb

So you can use Livecode with MogoDB as with any other database that uses ODBC connectivity protocol.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

javmirval
Posts: 9
Joined: Sun Feb 09, 2014 5:55 am

Re: Connecting and getting data from a local MongoDB databas

Post by javmirval » Mon Feb 10, 2014 4:55 pm

Thank you bery much Mark, so far I have been able to connect to my MongoDB. However when issuing a command to find document a:4 from test

I get:

MongoDB shell version: 2.2.7
connecting to: test
{
"_mongo" : connection to 127.0.0.1,
"_db" : test,
"_collection" : test.test,
"_ns" : "test.test",
"_query" : {
"a" : 4
},
"_fields" : null,
"_limit" : 0,
"_skip" : 0,
"_batchSize" : 0,
"_options" : 0,
"_cursor" : null,
"_numReturned" : 0,
"_special" : false,
"help" : function () ....

Instead of a document from the test db. The "same" command in the console executes OK.

Regards, Javier

javmirval
Posts: 9
Joined: Sun Feb 09, 2014 5:55 am

Re: Connecting and getting data from a local MongoDB databas

Post by javmirval » Mon Feb 10, 2014 4:58 pm

MaxV, thank you, I'll try the ODBC connector, for what they say in the Github it's still in early stages of development.

Regards, Javier

javmirval
Posts: 9
Joined: Sun Feb 09, 2014 5:55 am

Re: Connecting and getting data from a local MongoDB databas

Post by javmirval » Mon Feb 17, 2014 12:48 pm

Hi friends, first please excuse me for this lengthy post.
New to collections, documents etc. I have reached a point in which I cannot go any further, something bad considering the powerful tools I got at hands.
I have installed mongo in my PC and its running OK. I can interact with it, populating the test DB, and making some queries. In LC I have created a stack with:

One button that puts the received document into a field. I understand that I have to use DG to receive the data, but for now I am using a regular field.
One field “A” to receive the response from mongo
One field “B” to with the parsed string that I put in fld “A” -- You’ll see why I am doing this later.

The script for the button is:
on mouseup
set the hideConsoleWindows to true
put shell("C:\\mongodb\bin\mongo.exe --eval" && quote & "printjson(db.test.findOne())" & quote) into pJSON
--put shell("C:\\mongodb\bin\mongo.exe --eval" && quote & "printjson(db.test.find())" & quote) into pJSON
put pJSON into fld "a"
put offset("{",pJSON) into desde
put the length of pJSON into largo
put char desde to largo of pJSON into pJSON
put pJSON into fld "B"
put JSONToArray(pJSON) into tArray
put tArray["a"]
end mouseup


function JSONToArray pJSON
local tArray,tKeys
repeat for each line tKey in mergJSONDecode(pJSON,"tArray")
put JSONToArray(tArray[tKey]) into tArray[tKey]
end repeat
return tArray
end JSONToArray

Please take note on all the string manipulation I did to isolate the document from the response of the server, mouseup converts this:
MongoDB shell version: 2.2.7
connecting to: test
{ "_id" : ObjectId("52e3f87c8da8b1efb07004c9"), "a" : 1 }

To this:
{ "_id" : ObjectId("52e3f87c8da8b1efb07004c9"), "a" : 1 }

After I “parsed” the original response from the server I thought I produced a JSON, but validating it with an online service, I discovered that it really isn’t a JSON. That is why the function JSONToArray(pJSON) fails . I think this string manipulation is not needed and the problem is that I am doing something wrong retrieving data from the server.
What I described is one of the problems I found, the other happens when in the mouseup handler,
I replace:
put shell("C:\\mongodb\bin\mongo.exe --eval" && quote & "printjson(db.test.findOne())" & quote) into pJSON

With
put shell("C:\\mongodb\bin\mongo.exe --eval" && quote & "printjson(db.test.find())" & quote) into pJSON

This time fld “A” gets:
MongoDB shell version: 2.2.7
connecting to: test
{
_"_mongo" : connection to 127.0.0.1,
_"_db" : test,
_"_collection" : test.test,
_"_ns" : "test.test",
_"_query" : {
__
_},
_"_fields" : null,
_"_limit" : 0,
_"_skip" : 0,
_"_batchSize" : 0,
_"_options" : 0,
_"_cursor" : null,
_"_numReturned" : 0,
_"_special" : false,
_"help" : function () {
print("find() modifiers");
...
...
...
}

Seems printjson(db.test.find()) is returning some kind of description, instead of the documents test contains.
Any comments/suggestion will be appreciated,
Saludos, Javier

Post Reply

Return to “Databases”