27

I am trying to use MongoDB with just javascript from client, but MongoDB's documentation on how to achieve this is very confusing.

On this webpage there is nothing to download, I was expecting to see something like mongo.js.

Here I did find mongo.js, and using this I am trying to make it work but with no luck.

The Javascript console in Google Chrome is saying:

Uncaught TypeError: Object [object Object] has no method 'init'

In this snippet from mongo.js:

if ( typeof Mongo == "undefined" ){
  Mongo = function( host ){
    this.init( host );  
  }
}

Does anyone have any tips on using MongoDB with pure Javascript?

4

3 回答 3

29

您链接到的文档是关于使用 node.js 框架使用服务器端Javascript访问 MongoDB 。

MongoDB 确实提供了一个 REST Web 服务,允许通过 XmlHttpRequests 进行基本查询。要启用它,您必须使用--rest参数启动 mongod。然后,您可以像这样查询它:

http://127.0.0.1:28017/yourDatabase/yourCollection/?filter_name=Bob

您可以像任何 Web 服务一样使用 AJAX XmlHttpRequest 查询此 URL。它将访问 localhost 上的数据库并返回等效于如下查询的 JSON:

yourDatabase.yourCollection.find({name:"Bob"});

然而,这个界面非常简陋。它只提供简单的查找查询。但是有 3rd 方中间件层可以提供更高级的功能。此处记录了此功能和第 3 方解决方案列表:

http://docs.mongodb.org/ecosystem/tools/http-interfaces/

于 2013-04-29T11:32:19.730 回答
5

更新:MongoDB 今年推出了一项服务,MongoDB Stitch。这允许开发人员连接到 MongoDB Atlas(云)并公开数据以及直接在 ui 上使用的查询(通过 js)。目前,它处于测试阶段,但文档和示例在他们的网站上供参考。

于 2017-10-04T07:08:11.377 回答
3

There are lots of limitations in using REST web services provided by MongoDB. It is having very limited functionality and we can not provide query criteria or sort options while querying the data.

I suggest to write your own server side script or servlet to provide REST interface to fetch the data from MongoDB.

于 2013-09-20T17:51:46.220 回答