0

How to make a Mojito application connect to MongoDB database?

Hi,

I am really new to Mojito. I am trying to see if Mojito has the ability to use anything other than YQL. I am looking to create a customized application which talks to an offline database (using MongoDB). I want to know a step by step way to have the application connect to MongoDB in the model and then query the database to get data from database. Also how would the application write data into the database?

I am looking for a detailed example how to setup such an environment? Is there a detailed step by step documentation for the same? From Mojito's documentation and code examples it seems everywhere they are using YQL and I don't have a need for that.

Any help would be really appreciated

Thanks, KA

by
  • k
  • Apr 16, 2013
3 Replies
  • Hey KA, mojito does not enforce the use of YQL, we use it in the examples and demos for convenience, and since it reflects the way to access data from another layer, and that's all that really matters, we think that's enough, but maybe it is not :)

    Anyway, you should be able to use Y.io or request or any other nodejs module in your model to connect to any api or database, just like we do with YQL. Models are very simple structures, they just have methods that can receive some arguments and a callback, whatever you do internally, it is just javascript. If your model has server affinity, then it is even easier because you can require any nodejs module that implements mongodb or any other type of data source.

    If you have any specific question, we can try to answer it, but at the moment we don't have any step by step example for accessing MongoDB.

    0
  • Hi, Caridy forgive my ignorance but how can I load any other (than YUI) nodejs module in model? Simply put nodejs module name in "requires" array seems not work eg

    , {requires:['mojito', 'mongodb']});

    I put the name into 'package.json' dependencies and installed 'mongodb' inside app directory ie npm ls

    lists

    <mongodb@1.3.19> and its dependencies (<bson@0.2.2>, <kerberos@0.0.3>)

    but after

    mojito start

    I have:

    ...

    warn: (yui): NOT loaded: mongodb

    Regards Darek

    0
  • well, nodejs modules are only available on the server side. So, if your model is meant to be used on the server side only (aka *server.js), then you can do:

    ``` YUI.add('foo', function (Y, NAME) {

    var mongo = require('mongodb');
    

    }, '', {requires:['mojito']}); ```

    in the other hand, if you need to use a different library for client, you can probably create two models (that can potentially use common module that implements the shared pieces) and each model use the corresponding library, etc.

    1

Recent Posts

in Yahoo! Mojito