使用Javascript从客户端使用MongoDB [英] Using MongoDB from client with Javascript
问题描述
我正在尝试仅将MongoDB与客户端的javascript一起使用,但是MongoDB的有关如何实现此目的的文档非常令人困惑.
I am trying to use MongoDB with just javascript from client, but MongoDB's documentation on how to achieve this is very confusing.
在此网页上,没有可供下载的内容,我期望看到一些内容像mongo.js.
On this webpage there is nothing to download, I was expecting to see something like mongo.js.
这里我确实找到了mongo. js,并使用此使它工作,但没有运气.
Here I did find mongo.js, and using this I am trying to make it work but with no luck.
Google Chrome浏览器中的Javascript控制台说:
The Javascript console in Google Chrome is saying:
未捕获的TypeError:对象[object Object]没有方法'init'
Uncaught TypeError: Object [object Object] has no method 'init'
在mongo.js的这段代码中:
In this snippet from mongo.js:
if ( typeof Mongo == "undefined" ){
Mongo = function( host ){
this.init( host );
}
}
有人对使用纯Javascript使用MongoDB有任何提示吗?
Does anyone have any tips on using MongoDB with pure Javascript?
推荐答案
您链接到的文档是关于使用服务器支持的Javascript使用node.js框架访问MongoDB.
The documentation you linked to is about accessing MongoDB with server-sided Javascript using the node.js framework.
MongoDB确实提供了REST Web服务,允许通过XmlHttpRequests进行基本查询.要启用它,您必须使用--rest
参数启动mongod.然后,您可以像这样查询它:
MongoDB does offer a REST webservice allowing rudimentary queries through XmlHttpRequests. To enable it, you have to start mongod with the --rest
parameter. You can then query it like this:
http://127.0.0.1:28017/yourDatabase/yourCollection/?filter_name=Bob
您可以像使用任何Web服务一样,使用AJAX XmlHttpRequest查询此URL.它将访问本地主机上的数据库,并返回等效于如下查询的JSON:
You can query this URL with an AJAX XmlHttpRequest like any webservice. It will access a database on localhost and return JSON equivalent to a query like this:
yourDatabase.yourCollection.find({name:"Bob"});
但是,此界面非常基本.它仅提供简单的查找查询.但是,有第三方中间件层可以提供更多高级功能.此处记录了此功能和第三方解决方案的列表:
This interface, however, is very rudimentary. It only offers simple find queries. But there are 3rd party middleware layers which expose more advanced functionality. This feature and a list of 3rd party solutions is documented here:
http://docs.mongodb.org/ecosystem/tools/http-interfaces /
这篇关于使用Javascript从客户端使用MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!