Introduction to mongodb - adasilva/waterYourWallet GitHub Wiki
In this page, we'll set up a local database and learn some of the pymongo commands needed to access it. When you're ready, go to Instructions on the ObjectRocket database.
It might be useful to work on a local database for practice. To set up a database:
- Install mongodb
- Install pymongo
- Start up the database (link to documentation)
in the terminal:
mongod --dbpath /srv/mongodb/
If you leave out --dbpath
, the default path is /data/db
(C:\data\db
on windows), which did not exist on my computer. You can use any folder you want, but it must already exist!
First, start up your local database (see above). The following is based on two tutorials: Tutorial from pymongo site; Another mini-tutorial.
First, import pymongo and create a connection to the database (you should already have started mongo).
import pymongo
conn = pymongo.Connection('localhost')
Next, create a test database. This one is called test_database
db = conn.test_database
Each database contains a set of collections. Think of collections as the tables of a traditional database. We can check which collections exist in a database by using the collection_names
function:
db.collection_names()
Right now this returns an empty list because we haven't set up any collections yet. Let's do that now:
collection = db.test_collection
db.collection_names()
We still get an empty list! This is because pymongo is "lazy" - it only creates collections when there's something to put in it. So now we insert something. The items contained in the collections are dictionaries.
collection.insert({'Name':'Ashley','quote':'hello world!'})
db.collection_names()
The insert function returns an object ID. Now our database has our test_collection
in it! The data is now in the database; go ahead and exit python and restart.
import pymongo
conn = pymongo.Connection('localhost')
db = conn.test_database
db.collection_names()
And the collection you just created should still be there!
You can find one item which matches a given query using the `find_one() method on your test collect. For example, let's say I want to find one user who's name is Ashley:
db.test_collection.find_one({'Name':'Ashley'})
You should see the dictionary of values that you set, along with a unique object ID which was automatically created. It might be the case that more than one user has the same value for Name, and we want to find all that match. To do this, we use find
, which is an iterator. This means that the return value of find()
is a cursor, not a list! But you can easily iterate over,
for item in db.test_collect.find({'Name':'Ashley'}):
print item
If you try find_one
or find
on something which doesn't exist,
db.test_collection.find_one({'Name':'aShley'})
it should return nothing (None
). To perform more complicated searches, one can use python regular expressions package, re
import re
Using re.compile
, you create a string. Then pass this to the find
or find_one
methods:
exp = re.compile('^ash', re.IGNORECASE)
db.test_collection.find_one({'Name':exp})
The above example will find all entries where 'Name' starts with 'ash'. Passing re.IGNORECASE
in the compile
function means exactly that: it will ignore upper/lower cases.
First update your local git repository with new branches from github using
git fetch
This grabs the data on old and new branches without merging them together. Then switch to the master branch using checkout:
git checkout master
Create a new branch in your local water your wallet folder based off the master branch; below I've named it mydatabase, but you can call it something different.
git branch mydatabase
Then enter the new branch using checkout
git checkout mydatabase
Take a look at the file local_database_setup.py
. This will create a database in your local mongodb, with one collection containing a few plants. Run it using
python local_database_setup.py
If you run into an error, make sure you started up your local mongodb first!
Todo: revise code to allow one to use a local database, and give sample configuration files for the local database setup.
When you're ready to push to github,
git push origin mydatabase
(where the name mydatabase is whatever you called it.)