Mongodb client side javascript api

浪尽此生 提交于 2019-12-04 17:03:45

问题


I would like to use mongodb directly from the client-side, using javascript inside my models. I suppose this is possible since on the mongo website there is a javascript realtime console for demo.

Does exist a way to use the same api in a normal javascript application without having to write client-server glue code?

Thanks


回答1:


Looks like the site uses REST-like AJAX interface, but I am certain that it does not access any MongoDB instance directly or even indirectly via some bridge.

Thinks twice before applying this pattern: how will you enforce security? Are you aware of AJAX limitations? If you really know what you are doing, the official documentation points to few useful resources:

  • Sleepy Mongoose (Python) is a full featured REST interface for MongoDB which is available as a separate project.

  • MongoDB Rest (Node.js) is an alpha REST interface to MongoDB, which uses the MongoDB Node Native driver.

  • Simple REST Interface The mongod process includes a simple read-only REST interface for convenience. For full REST capabilities we recommend using an external tool such as Sleepy.Mongoose.

Having a REST bridge and managing the same origin policy you can easily access MongoDB directly using AJAX calls and JavaScript.




回答2:


I needed to do something similar to this myself, a console inside an admin interface, and since I use PHP in the back end I just made script to receive the js commands and use MongoDB::execute method, the connection in php is already open so I didn't have to pass any authentication data, just the commands.
You have to be extremely careful with something like this, though, and make sure only trusted users can access the page (and script), don't make it publicly available since anyone can execute js on the client to gain access to your db.

Edit: Here's the link to the docs http://ar2.php.net/manual/en/mongodb.execute.php




回答3:


Take a look at meteorjs, or similar. Models written once, complete ACL.

Gets even better when you add in the Redis :)




回答4:


For anyone else that's wondering, this is possible using Stitch which is similar to Google's Firebase, in that it provides clientside access to the backend database which in this case is MongoDB on Atlas BaaS. Stitch also has functions like AWS Lambdas and other features that make sense for developing real-time apps. Seemed fairly straight forward when I set up the basic blog tutorial yesterday at the meetup. Cheers!

https://docs.mongodb.com/stitch/tutorials/



来源:https://stackoverflow.com/questions/7292648/mongodb-client-side-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!