简体   繁体   English

使用来自客户端的 MongoDB 和 Javascript

[英]Using MongoDB from client with Javascript

I am trying to use MongoDB with just javascript from client, but MongoDB's documentation on how to achieve this is very confusing.我正在尝试将 MongoDB 与来自客户端的 javascript 一起使用,但是 MongoDB 关于如何实现这一点的文档非常混乱。

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. 在这里我确实找到了 mongo.js,并且我试图使用来让它工作但没有运气。

The Javascript console in Google Chrome is saying: Google Chrome 中的 Javascript 控制台显示:

Uncaught TypeError: Object [object Object] has no method 'init'未捕获的类型错误:Object [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?有没有人有关于将 MongoDB 与纯 Javascript 一起使用的任何提示?

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. MongoDB提供了一个REST Web服务,允许通过XmlHttpRequests进行基本查询。 To enable it, you have to start mongod with the --rest parameter. 要启用它,您必须使用--rest参数启动mongod。 You can then query it like this: 然后,您可以这样查询:

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

You can query this URL with an AJAX XmlHttpRequest like any webservice. 您可以像任何Web服务一样使用AJAX XmlHttpRequest查询此URL。 It will access a database on localhost and return JSON equivalent to a query like this: 它将访问localhost上的数据库并返回相当于这样的查询的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: 此功能和第三方解决方案列表在此处记录:

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

if you want to do that from a web browser, try meteor, it allows client side access to mongo 如果你想从网络浏览器那样做,试试meteor,它允许客户端访问mongo

see http://meteor.com/ http://meteor.com/

and a demo http://meteor.com/screencast 和演示http://meteor.com/screencast

There are lots of limitations in using REST web services provided by MongoDB. 使用MongoDB提供的REST Web服务有很多限制。 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. 我建议编写自己的服务器端脚本或servlet来提供REST接口以从MongoDB获取数据。

Update: MongoDB has a service introduced this year , MongoDB Stitch . 更新:MongoDB今年推出了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) . 这允许开发人员连接到MongoDB Atlas(云)并公开数据以及直接在ui(通过js)使用的查询。 Currently, its in beta but documentation and samples are on their site for reference. 目前,它的测试版但文档和样本都在他们的网站上供参考。

This should be possible now with Realm.现在使用 Realm 应该可以做到这一点。 https://docs.mongodb.com/realm/web/quickstart/ https://docs.mongodb.com/realm/web/quickstart/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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