Writing arbitrary mongoDB queries to PHP backend - mongodb

Im in the start up phase of creating an internal system based on PHP and MongoDB. The users of this system are Javascript programmers and would like to be able to make custom queries to the Mongo database from a frontend gui with arbitrary Mongo shell queries. Of course this would not be a problem at all if I forced them to to write the queries with proper PHP arrays etc, but i would definitely like to avoid this.
I am not quite sure how to approach a feature like this without writing some advanced methods being able to restructure the queries to proper formated arrays that can be used in MongoClient PHP. One approach would be making use of the i.e. MongoDB::execute() method and run the javascript on the database server - a method i don't fancy at all.
Im kindly asking if you have any ideas on how to achieve the requested functionalities to some extend.
Thank you in advance.

Are you looking for something like this : http://rockmongo.com/ ?

Related

How to connect simple website to RDS PostgreSQL DB

This is possibly a broad question:
I've been teaching myself how to program for the last year. I've learned how to create simple websites using HTML, CSS, and JavaScript. I've also learned how to create simple databases using PostgreSQL. I need help figuring out how to connect (API) the front end and the back end.
A friend suggested I learn how to use AWS RDS so I've already created a PostgreSQL DB instance there (tutorial). I've also connected to my DB using pgAdmin 4 using this tutorial.
What I would like to do is have my front end take in a number as the user's input and "submit" (POST) that value in the DB. Every time the user submits a new value, the new value is saved in the DB and the cumulative values of everything POSTed is returned to the front end for the user to see.
I know this project is a bit asinine but its a start. I can't find any more links on the internet to teach myself how to do this. If you have tips or resources (web links) to help me accomplish this, I would be really grateful.
Side note - I've also been studying python for the last year if that helps.
First, connect to the database like so:
$c1 = new PDO('pgsql:host=THERDSURL;user=THEDBUSERNAME;dbname=THEDATABASENAME;password=THEPASSWORD;connect_timeout=500',null,null);
Then start a new query like so:
$statement=$c1->prepare("select * from mytable");
Then execute it:
$statement->execute();
then loop through the results like so:
while($row=$statement->fetch());
{
var_dump($row)
}
it will vary a little depending on whether you're getting data or inserting data, but begin by learning how to get (select) data. Create a table using a GUI of some kind and learn how to query it programatically.
NOTE: Most people use a library of some kind to make the querying both more powerful and less complicated.. This code is in PHP, but a very similar process is done in many languages.
Note2: do not try to do any of this in javascript that's embedded in html or that's running on a client machine. Connecting to postgres using embeded javascript is not the normal way to go about doing this. Usually people use a language like PHP, Python, Java, etc.. and that's usually running on the server-- and although you can use javascript that runs on the server, I wanted to warn you about this because newer developers might not realize that javascript running on the server vs the browser has completely different capabilities where this is concerned.

How to visualize data from a Postgresql in Kibana?

I need to some visualize data from a Postgresql in Kibana. I have also ElasticSearch installed just in case. So how visualize data from a Postgresql in Kibana? Of course, I don't need the whole database, but only data returned by a custom sql query.
Also, I want it to be as simple as possible, I wouldn't like to use libraries I really don't need to use.
Kibana was built with Elastisearch in mind.
Having used it quite a lot in a startup I worked for, I can tell you that even the front-end query DSL (built on Lucene) will only work with Elasticsearch (or might need some serious tweaks).
I would advise you to push your data into Elasticsearch, and just work with Kibana the way it was made for :)

Stringify object for qs parser

Express js uses qs parser to parse query strings into objects. Is there a lib out there for use on front-end applications that does the reverse? I would love to be able to write a mongo db query on the front end in object form and have it parsed automatically into the format qs parser is expecting right before the request is made.
I would love to be able to write a mongo db query on the front end in object form
You should absolutely not do this unless you're OK with essentially giving any user of your web app an open shell to do whatever they want to your database.
But with that said, I found this in bower.io:
https://github.com/fernandofleury/query-object
More here:
http://bower.io/search/?q=qs
Stumbled upon this question, I suppose you don't need it anymore, but I hope this helps other users who end up here:
Meet graphQL. It's an open standard, which lets you query backend api's in a flexible manner.
Mongodb even supports it out of the box and it is permission based, so no worries about frontend users hacking into your database (you should of course properly configure those permissions to limit access to specific resources).
Read more on https://docs.mongodb.com/realm/graphql

Zend MongoDB implementation

I am attempting to integrate Zend with MongoDB. I think I am almost there, but I need some help to complete the task. I am new to both zend and mongo, but reasonably experienced with PHP and MySQL.
Some details:
My Zend project is based on the skeleton tutorial at http://framework.zend.com/manual/2.0/en/user-guide/skeleton-application.html. This works fine on my computer, but is mysql-based.
I have installed MongoDB. I can write nosql on the command line and I can instantiate a MongoClient object in a PHP script, so those things seem to work and talk to each other.
I want to use mongo instead of mysql, and I have implemented Doctrine. I have no idea what Doctrine does, and I've been through several searches to try and find how to run mongo commands in zend. These facts lead me to conclude that I don't know what I'm doing.
So to the specifics of my question:
As I understand it, I should be instantiating a mongo object somewhere. If so, where should I put the instantiation in zend?
If not, presumably I need an existing instantiation of the mongo object to run various find/save commands from inside the model class, so if that's true, how do I access the existing mongo object?
Thanks for any help, and clarification questions are equally welcome.
Edited 13/12/2012 for clarification.
With the given answers it seems I am on the right track, and I can further clarify what I need:
Looking at the Doctrine Documentation linked below, I am still unclear where the document manager instantiation is supposed to occur. I would have expected it to go into a more generic location which is run on every page load. Also, most of the tutorials I find seem to refer to a file structure different given from the skeleton application, so when they say to put certain things in certain places, I'm not sure where they mean. For example, I keep seeing references to bootstrap files, but my only bootstrap files are in test directories.
My questions are these:
What code do I need to add into the indexAction function (which definitely responds when I go to the correct URL) if I simply want to print_r all the documents in a collection?
Do I need to add any other code elsewhere, if so where, and what code?
These are the different bits and what they do, working from the bottom up:
mongodb: data persistance
doctrineODM: Object Document Mapper. This maps mongo documents to php objects. It means you don't have to worry about doing any calls against mongo directly - you just work with plain old php objects. To use doctrineODM you need to define your model classes, and get the hang of using the DocumentManager - suggested, the Doctrine docs are quite good.
doctrineMongoODMModule: A module that integrates doctrine with zf2. The main thing it does is allow you to configure doctrine using zf2 config style, and gives you access to the DocumentManager via the zf2 ServiceManager. (I does quite a few other things too, but they are the main ones)
zf2 (mvc): Lets you define an interface so other things (like a web client) can manipulate your documents.
There is brilliant module doctrine-mongo-odm-module for mongoDB access.
Just add to composer.json
"require": {
....
"doctrine/doctrine-mongo-odm-module": "dev-master",
....
Doctrine documentation should help how to use it.

Is there a better way to use javascript to import of data from an external URL into mongo db

The best I can come up with is to use the mongo shell "run" command to:
execute curl write to pull $someUrl to fs/tmp/myfile.ext
execute mongoimport to load fs/tmp/myfile.ext
I don't know the file system structure at compile time, so I am hoping the Gurus may know of a more direct way of loading data from a remote URL.
You can do this quite easily with any of the languages or drivers.
So if you use PHP + cURL + MongoDB, you can basically store the response right into MongoDB with all of the features of cURL.
The same should be true for most dynamic languages Python, Ruby, etc. If you use Node.js, the process should be quite similar. I only suggest the dynamic language for ease of scripting, but pretty much any of the supported drivers should allow you to do this.
Frankly storing it in a file is probably unnecessary.