Querying MongoDB from browser using a flask backend - mongodb

I am building an interactive visualization tool that lets users query a database which is then visualized using D3, Flask as the server and MongoDB as the database. My question: How do I query the MongoDB (from Flask) with the user input and render this to the server?

Install the mongodb and run its server (in terminal type mongo)
Install pymongo (python package)
Then create an instance of the pymong using:
from pymongo import MongoClient
client = MongoClient(MONGO_URL)
db = getattr(client, DATABASE_NAME)
4. Then you can query using following:
> documents = db.your_collection.find()
> db.your_collection.insert({'name': 'Nabin Khadka'})
These python code can be wrapped in views.py file under a method. Like:
#app.route('/test')
def test():
# All above code
return jsonify(response_dictionary)
Then running the app, we can call the following url from browser:
https://url_to_server:port/test

Related

Running pymongo to save data in local mongoDB database

I was trying to save data from jupyter notebook in Visual Studio Code into my local mongoDB database. Previously, I had multiple databases in my local mongoDB server. However, after running the pymongo code, all the existing databases got deleted and only the new database that was created in the python code exists.
Is there a reason because of which this has occurred? How can I resolve it?
Here's the code I had used for the same:
# Making a Connection with MongoClient
client = MongoClient("mongodb://localhost:27017/")
# database
db = client["stocks_database"]
# collection
company= db["Company"]
db.list_collection_names()
company.insert_one({"index":"Sensex","data":'abc'})

Using external mongodb with meteor development

A Meteor server code uses Accounts-ui package gets started like this:
appDir$ MONGO_URL=mongodb://username:password#cloud-host-url meteor
After creating the account, the user document fails to show up in the mongodb on the cloud-host but shows up in the local mongodb copy.
How can I correctly use the cloud mongodb?
I think the problem is how you set your MONGO_URL, try this in your CLI:
export MONGO_URL=mongodb://username:password#mongo_link && meteor

Mongodb "auth fails" with mongodb php driver and new php library

Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.
In the following code, the connection string follows the pattern mongodb://user:password#mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.
However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179
Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.
Environment:
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6
I just had the same error and found out that I had to place the database-name in the connection string.
The documentation here says:
If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
And the user I'm using has no rights to the admin-database, so this is why I received the authentication error.
I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.
So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.

Connecting to Remote MongoDB Server with Python

I have a script that I wrote to query mongodb in python I am using PyMongo. I am trying to use this script to connect to a remote MongoDB server and then run the query within the script and then I want to be able to dump the data I get back from the mongodb into a file.
What are the parameters I need to have at the top of the script to connect to this database, use my username and password, switch to the correct database and then run the query?
Couple of options.
First you could provide a MongoDB URI which can be provided to the MongoClient as an argument. Then you can switch as needed using the standard methods for getting a database once connected.
Alternately, you can connect as normal, use the getting a database once connected method to get the desired database and then use the authenticate function to authenticate against the database.

MongoDB w/ PyMongo on Heroku -- error: no module named pymongo

I am making a heroku app using Flask with a mongoDB backend. I am using pymongo as my driver but when I push my code to git, it crashes and spits out this error:
2014-04-05T09:56:36.301695+00:00 app[web.1]: ImportError: No module named pymongo
The web-app works locally and pymongo works completely fine in that scenario. What do I have to do to have pymongo recognized on heroku's servers?
Thansks!
You need to make Heroku aware of PyMongo as a requirement. The easiest way to do this is my adding pymongo==x.x to your requirements.txt file.
If you are using a setup.py script instead of requirements.txt, add 'pymong==x.x', to the install_requires argument to setup.
In both cases, make sure to replace x.x with the appropriate version number.