简体   繁体   中英

Client-side jQuery application with MongoDB

I'm trying to write a very simple example application to familiarize myself with using MongoDB. Essentially, I'd like to have a single web page which queries a local MongoDB server, adding and removing content dynamically using jQuery. I have no problem at all throwing together the page layout and the jQuery, but I'm getting more and more confused by the MongoDB part of the equation. I understand that MongoDB is a server and runs remotely from the client, but for my example, I simply want to be able to query quickly and easily from client-side in-browser JavaScript:

$("#toggle").click(function() {
    if ($(this).is(":checked") {
        // add items from mongodb
        addItems(mongodb.test.find({ age: { $gt: 5 }}));
    } else {
        $("#results").hide();
    }
});

Is there a way to interface with MongoDB this way?

You need a driver to connect to a MongoDB server. The list of drivers is here: http://www.mongodb.org/display/DOCS/Drivers

There is a JS driver, but only for server side JS - specifically node.js

Bottomline, you can't connect directly from a browser. You need a server side component.

As @balafi states you need a driver.

MongoDB does have a REST interface and infact there are drivers such as Mongoose that designed to create a fully functional REST interface for MongoDB.

This could be the route to go if you want to use MongoDB without all the hassle of setting up a server end. This way you would just ping a POST or GET call from JQuery with the specified params you want.

You can find more information on the REST interfaces here: http://www.mongodb.org/display/DOCS/Http+Interface

However I should warn you the built in one for MongoDB is extremely lacking and is only designed for extremely simple queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM