Exemple #1
0
class MongoProfile(TestCase):

    expected_records = [
        '==== insert ====',
        'test> db.people.insert({...})',
        'test> db.people.insert({...})',
        '==== modification ====',
        'test> db.people.update({ name: "John" }, {...})',
        'test> db.people.remove({ name: "Mary" })',
        '==== search ====',
        'test> db.people.find({ $query: { age: { $gt: 20.0 } } })',
        'test> db.runCommand({ count: "people", query: { age: { $gt: 20.0 } }, fields: null })',
    ]

    def setUp(self):
        self.db = Connection().test

    def testMongoProfile(self):
        profiler = MongoProfiler(self.db)
        self._doQueries(profiler)
        record_info = [str(record) for record in profiler.get_records()]
        for expected, received in zip(self.expected_records, record_info):
            self.assertEquals(expected, received)

    def testDummyMongoProfile(self):
        profiler = DummyMongoProfiler(self.db)
        self._doQueries(profiler)
        self.assertEquals(profiler.get_records(), [])

    def _doQueries(self, profiler):
        with profiler:
            profiler.mark('insert')
            self.db.people.insert(dict(name='John', age=20))
            self.db.people.insert(dict(name='Mary', age=30))
            profiler.mark('modification')
            self.db.people.update({'name': 'John'}, {'age': 21})
            self.db.people.remove({'name': 'Mary'})
            profiler.mark('search')
            list(self.db.people.find({'age': {'$gt': 20.0}}))
            self.db.people.find({'age': {'$gt': 20.0}}).count()

    def tearDown(self):
        self.db.drop_collection('people')
Exemple #2
0
def remove_authors():
    db = Connection().poembot
    db.drop_collection("authors")
Exemple #3
0
def remove_tokens():
    db = Connection().poembot
    db.drop_collection("tokens")
Exemple #4
0
def remove_characters():
    db = Connection().poembot
    db.drop_collection("characters")
Exemple #5
0
def remove_templates():
    db = Connection().poembot
    db.drop_collection("templates")
Exemple #6
0
def remove_poems():
    db = Connection().poembot
    db.drop_collection("poems")
Exemple #7
0

# Syslog-ng will support MongoDB from version 3.3

from datetime import datetime
import socket
import sys
from time import time

from pymongo import Connection

db = Connection().logaar

incoming = db.incoming
if 'capped' not in incoming.options():
    db.drop_collection('incoming')
    db.create_collection("incoming", capped=True,size="100000")
    incoming = db.incoming

port = 514

def inject(data):
    # <19>Mar 22 21:19:01 ehm nullmailer[1727]: Sending failed:  Connection timed out

    msg = {
        "host": "localhost",
        "msg": data,
        "tags": [],
        "date": datetime.utcnow(),
        "level": "info",
    }
Exemple #8
0
import pprint
from bson.son import SON
from pymongo.code import Code
from pymongo import Connection

"""
Example of merging two map reduce 
data using reduce out
"""

db = Connection().map_reduce_example

db.drop_collection('users')
db.drop_collection('comments')
db.drop_collection('users_comments')

db.users.insert({'firstName':"Rich",'lastName':"S",'gender':"M",'country':"CA",'age':"18"})
db.users.insert({'firstName':"Rob",'lastName':"M",'gender':"M",'country':"US",'age':"25"})
db.users.insert({'firstName':"Sarah",'lastName':"T",'gender':"F",'country':"US",'age':"13"})

users = db.users.find({})

db.comments.insert({'userId': users[0]['_id'], "comment": "I am rich?"})
db.comments.insert({'userId': users[0]['_id'], "comment": "I am going to be rich?"})
db.comments.insert({'userId': users[0]['_id'], "comment": "Here i come rich?"})
db.comments.insert({'userId': users[0]['_id'], "comment": "Go away rich?"})
"""
db.comments.insert({'userId': users[1]['_id'], "comment": "Not much"})
db.comments.insert({'userId': users[0]['_id'], "comment": "Cool"})
"""
Exemple #9
0
import requests
import json
import time
from pymongo import Connection

SERVER_URL = 'http://*****:*****@gmail.com'
    })
Exemple #10
0
indexes).

“MongoDB: The Definitive Guide by Kristina Chodorow and Michael Dirolf 
(O’Reilly). Copyright 2010 Kristina Chodorow and Michael Dirolf, 
978-1-449-38156-1.”

"""

from __future__ import print_function

import time
from pymongo import Connection
db = Connection().naadam

#collection = db.create_collection('megaints')
db.drop_collection('megaints')
collection = db.megaints

start_t = time.time()
for i in xrange(100000):
    collection.insert({"foo": "bar", "baz": i, "z": 10 - i})
insert_t = time.time() - start_t
print('%0.3f seconds to insert' % insert_t)

start = time.time()
collection.remove()
collection.find_one()
total = time.time() - start
print('%0.3f seconds to remove' % total)