Pymongo: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
==Here We Go== | |||
On this page we show how to set up a connection to the MongoDB. | |||
Fire up mongodb: | |||
<pre> | |||
$ mongod -f /usr/local/etc/mongodb.conf | |||
</pre> | |||
Fire up Python: | |||
</ | <pre> | ||
>>> from pymongo import MongoClient | |||
>>> client = MongoClient('localhost', 27017) | |||
</pre> | |||
Now you're connected to the local MongoDB daemon, and you can interact with the database. | |||
If you have a MongoDB client, you must start by getting a database (create one if it does not exist), in this case one called test_database: | |||
<pre> | |||
>>> db = client.test_database | |||
</pre> | |||
Now that you have a database, you can get a collection (create one if it does not exist): | |||
<pre> | |||
>>> collection = db.test_collection | |||
</pre> | |||
and finally, once you have a collection you can start to add documents to it: | |||
<pre> | |||
>>> doc = { 'bssid' : 'aa:bb:cc:dd:ee:ff:00:11', 'channel' : 5, 'ssid' : 'Nacho Wifi', 'strength' : -20, 'encryption' : 'WPA' } | |||
>>> | |||
</pre> | |||
(SQL analogy: once you have a database, you can start to add tables. Once you have tables, you can start to add records/rows to the tables.) | |||
To monitor the whole process, add the following line to your config file, so that mongodb will print out more info about what's going on: | |||
<pre> | |||
systemLog: | |||
path: /usr/local/var/log/mongodb/mongo.log | |||
verbosity: 2 | |||
</pre> | |||
Now, as you perform operations on mongodb in Python, you can run <code>tail -f /usr/local/var/log/mongodb/mongo.log</code> in another window, and monitor to ensure that everything is working as expected. | |||
Revision as of 23:59, 31 August 2016
Here We Go
On this page we show how to set up a connection to the MongoDB.
Fire up mongodb:
$ mongod -f /usr/local/etc/mongodb.conf
Fire up Python:
>>> from pymongo import MongoClient
>>> client = MongoClient('localhost', 27017)
Now you're connected to the local MongoDB daemon, and you can interact with the database.
If you have a MongoDB client, you must start by getting a database (create one if it does not exist), in this case one called test_database:
>>> db = client.test_database
Now that you have a database, you can get a collection (create one if it does not exist):
>>> collection = db.test_collection
and finally, once you have a collection you can start to add documents to it:
>>> doc = { 'bssid' : 'aa:bb:cc:dd:ee:ff:00:11', 'channel' : 5, 'ssid' : 'Nacho Wifi', 'strength' : -20, 'encryption' : 'WPA' }
>>>
(SQL analogy: once you have a database, you can start to add tables. Once you have tables, you can start to add records/rows to the tables.)
To monitor the whole process, add the following line to your config file, so that mongodb will print out more info about what's going on:
systemLog: path: /usr/local/var/log/mongodb/mongo.log verbosity: 2
Now, as you perform operations on mongodb in Python, you can run tail -f /usr/local/var/log/mongodb/mongo.log in another window, and monitor to ensure that everything is working as expected.