从客户端使用 MongoDB 和 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16276557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:10:54  来源:igfitidea点击:

Using MongoDB from client with Javascript

javascriptmongodb

提问by deloki

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

我正在尝试仅使用来自客户端的 javascript 来使用 MongoDB,但是 MongoDB 关于如何实现这一点的文档非常令人困惑。

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

这个网页上没有什么可以下载的,我期待看到类似 mongo.js 的东西。

HereI did find mongo.js, and using thisI am trying to make it work but with no luck.

在这里我确实找到了 mongo.js,并使用我试图让它工作但没有运气。

The Javascript console in Google Chrome is saying:

谷歌浏览器中的 Javascript 控制台说:

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

未捕获的类型错误:对象 [object Object] 没有方法“init”

In this snippet from mongo.js:

在 mongo.js 的这个片段中:

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

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

有没有人有关于使用纯 Javascript 使用 MongoDB 的任何提示?

采纳答案by Philipp

The documentation you linked to is about accessing MongoDB with server-sided Javascript using the node.js framework.

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

MongoDB does offer a REST webservice allowing rudimentary queries through XmlHttpRequests. To enable it, you have to start mongod with the --restparameter. You can then query it like this:

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

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

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:

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

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:

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

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

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

回答by Keith Nicholas

if you want to do that from a web browser, try meteor, it allows client side access to mongo

如果您想从 Web 浏览器执行此操作,请尝试 Metemet,它允许客户端访问 mongo

see http://meteor.com/

http://meteor.com/

and a demo http://meteor.com/screencast

和演示http://meteor.com/screencast

回答by Purushotham

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.

使用 MongoDB 提供的 REST Web 服务有很多限制。它的功能非常有限,我们无法在查询数据时提供查询条件或排序选项。

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

我建议编写自己的服务器端脚本或 servlet 来提供 REST 接口来从 MongoDB 获取数据。

回答by trashvin

Update: MongoDB has a service introduced this year , MongoDB Stitch. This allows developers to connect to MongoDB Atlas (cloud) and expose data as well as queries to be consumed directly at the ui ( through js) . Currently, its in beta but documentation and samples are on their site for reference.

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