From charlesreid1

(Created page with "<source lang="python"> pre_search = metaprops.find_one({'geoid':cbsa}) mongo_search = props.find({'$and': [ {'metroid...")
 
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
<source lang="python">
==PyMonGo On Get On With It Already==
        pre_search = metaprops.find_one({'geoid':cbsa})


        mongo_search = props.find({'$and': [
On this page we show how to set up a connection to the MongoDB.
                                            {'metroid':cbsa},
 
                                            {'geoid':{'$nin':[cbsa]}}
===Start mongodb===
                                            ]
 
                                })
Fire up mongodb on homebrew:
</source>
 
<pre>
mongod -f /usr/local/etc/mongodb.conf
</pre>
 
On Linux:
 
<pre>
mongod -f /etc/mongodb.conf
</pre>
 
===Start python===
 
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.
 
===Create database and collections===
 
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>
 
===Inserting data into collections===
 
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' }
>>> result = collection.insert_one(doc)
</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.
 
===Script: insert test data===
 
<pre>
from pymongo import MongoClient
 
client = MongoClient('localhost', 27017)
db = client.test_database
collection = db.test_collection
 
docs = [{ 'bssid' : 'A', 'channel' : 1, 'ssid' : 'asdf', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'B', 'channel' : 2, 'ssid' : 'qwer', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'C', 'channel' : 1, 'ssid' : 'woei', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'D', 'channel' : 1, 'ssid' : 'wori', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'E', 'channel' : 2, 'ssid' : 'oprr', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'F', 'channel' : 2, 'ssid' : 'fghf', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'G', 'channel' : 3, 'ssid' : 'yruy', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'H', 'channel' : 2, 'ssid' : 'aaaa', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'I', 'channel' : 4, 'ssid' : 'qaqw', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'J', 'channel' : 4, 'ssid' : 'sxv3', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'K', 'channel' : 1, 'ssid' : '8fkk', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'L', 'channel' : 2, 'ssid' : 'plnb', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'M', 'channel' : 4, 'ssid' : 'llzb', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'N', 'channel' : 3, 'ssid' : 'u2tt', 'strength' : -30, 'encryption' : 'WPA' }]
 
result = collection.insert_many(docs)
print result.inserted_ids
</pre>
 
===Script: print test data===
 
<pre>
from pymongo import MongoClient
 
client = MongoClient('localhost', 27017)
db = client.test_database
collection = db.test_collection
 
for doc in collection.find({'encryption':'WPA'}):
    print "Network %s has WPA encryption."%( doc['ssid'] )
</pre>
 
This should run very fast:
 
<pre>
$ python print_test_data.py
Network asdf has WPA encryption.
Network qwer has WPA encryption.
Network woei has WPA encryption.
Network wori has WPA encryption.
Network oprr has WPA encryption.
Network fghf has WPA encryption.
Network yruy has WPA encryption.
Network aaaa has WPA encryption.
Network qaqw has WPA encryption.
Network sxv3 has WPA encryption.
Network 8fkk has WPA encryption.
Network plnb has WPA encryption.
Network llzb has WPA encryption.
Network u2tt has WPA encryption.
</pre>
 
==Flags==
 
[[Category:MongoDB]]
[[Category:Python]]

Latest revision as of 20:50, 10 February 2018

PyMonGo On Get On With It Already

On this page we show how to set up a connection to the MongoDB.

Start mongodb

Fire up mongodb on homebrew:

mongod -f /usr/local/etc/mongodb.conf

On Linux:

mongod -f /etc/mongodb.conf

Start python

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.

Create database and collections

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

Inserting data into collections

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' }
>>> result = collection.insert_one(doc)

(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.

Script: insert test data

from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client.test_database
collection = db.test_collection

docs = [{ 'bssid' : 'A', 'channel' : 1, 'ssid' : 'asdf', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'B', 'channel' : 2, 'ssid' : 'qwer', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'C', 'channel' : 1, 'ssid' : 'woei', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'D', 'channel' : 1, 'ssid' : 'wori', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'E', 'channel' : 2, 'ssid' : 'oprr', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'F', 'channel' : 2, 'ssid' : 'fghf', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'G', 'channel' : 3, 'ssid' : 'yruy', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'H', 'channel' : 2, 'ssid' : 'aaaa', 'strength' : -20, 'encryption' : 'WPA' },
        { 'bssid' : 'I', 'channel' : 4, 'ssid' : 'qaqw', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'J', 'channel' : 4, 'ssid' : 'sxv3', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'K', 'channel' : 1, 'ssid' : '8fkk', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'L', 'channel' : 2, 'ssid' : 'plnb', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'M', 'channel' : 4, 'ssid' : 'llzb', 'strength' : -30, 'encryption' : 'WPA' },
        { 'bssid' : 'N', 'channel' : 3, 'ssid' : 'u2tt', 'strength' : -30, 'encryption' : 'WPA' }]

result = collection.insert_many(docs)
print result.inserted_ids

Script: print test data

from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client.test_database
collection = db.test_collection

for doc in collection.find({'encryption':'WPA'}):
    print "Network %s has WPA encryption."%( doc['ssid'] )

This should run very fast:

$ python print_test_data.py
Network asdf has WPA encryption.
Network qwer has WPA encryption.
Network woei has WPA encryption.
Network wori has WPA encryption.
Network oprr has WPA encryption.
Network fghf has WPA encryption.
Network yruy has WPA encryption.
Network aaaa has WPA encryption.
Network qaqw has WPA encryption.
Network sxv3 has WPA encryption.
Network 8fkk has WPA encryption.
Network plnb has WPA encryption.
Network llzb has WPA encryption.
Network u2tt has WPA encryption.

Flags