Python 2.7.11 |Anaconda 2.2.0 (64-bit)| (default, Dec 7 2015, 14:10:42) [MSC v.1500 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.


IPython 1.0.0 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.

%guiref -> A brief reference about the graphical user interface.


In [1]: import pymongo


In [2]: try:

   ...: client=pymongo.MongoClient("localhost",27017)

   ...: print "Connected Successfully!!!"

   ...: except pymongo.errors.ConnectionFailure,e:

   ...: print "Could not connect to MongoDB: %s " % e

   ...:

Connected Successfully!!!


In [3]: ticks_db=client.ticks_db


In [4]: clear



In [5]: import pymongo


In [6]: try:

   ...: client=pymongo.MongoClient("localhost",27017)

   ...: print "Connected Successfully!!!"

   ...: except pymongo.errors.ConnectionFailure,e:

   ...: print "Could not connect to MongoDB: %s" % e

   ...:

Connected Successfully!!!


In [7]: ticks_db=client.ticks_db


In [8]: ticks_db=client.ticks-db

Traceback (most recent call last):


File "<ipython-input-8-5d8b5440f2ca>", line 1, in <module>

ticks_db=client.ticks-db


NameError: name 'db' is not defined



In [9]: ticks_db=client['ticks-db']


In [10]: appl_collection=ticks_db.aapl


In [11]: aapl_collection=ticks_db['aapl-collection']


In [12]: import datetime as dt

    ...: tick={"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,0,0),

    ...: "open":115.58,

    ...: "high":`116.08,

    ...: "low":114.49,

    ...: "last":114.96,

    ...: "vol":1900000}

File "<ipython-input-12-4f11311bd4e5>", line 6

"low":114.49,

^

SyntaxError: invalid syntax



In [13]: import datetime as dt

    ...: tick={"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,0,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":114.49,

    ...: "last":114.96,

    ...: "vol":1900000}

    ...:


In [14]: tick_id=aapl_collection.insert(tick)

Traceback (most recent call last):


File "<ipython-input-14-b17de14588b0>", line 1, in <module>

tick_id=aapl_collection.insert(tick)


File "C:\Anaconda\lib\site-packages\pymongo\collection.py", line 2198, in insert

with self._socket_for_writes() as sock_info:


File "C:\Anaconda\lib\contextlib.py", line 17, in __enter__

return self.gen.next()


File "C:\Anaconda\lib\site-packages\pymongo\mongo_client.py", line 712, in _get_socket

server = self._get_topology().select_server(selector)


File "C:\Anaconda\lib\site-packages\pymongo\topology.py", line 141, in select_server

address))


File "C:\Anaconda\lib\site-packages\pymongo\topology.py", line 117, in select_servers

self._error_message(selector))


ServerSelectionTimeoutError: localhost:27017: [Errno 10061]



In [15]: tick_id=aapl_collection.insert(tick)


In [16]: print ticks_db.collection_names()

[u'aapl-collection']


In [17]: print tick_id

5688e2a7c589e52730b2e517


In [18]: print aapl_collection.find_one()

{u'last': 114.96, u'vol': 1900000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 0), u'_id': ObjectId('5687c5bcc589e503c48f1b26'), u'ticker': u'aapl'}


In [19]: print aapl_collection.fine_one({"time",dt.datetime(2014,11,17,10,0,0)})

Traceback (most recent call last):


File "<ipython-input-19-d2f34198bab1>", line 1, in <module>

print aapl_collection.fine_one({"time",dt.datetime(2014,11,17,10,0,0)})


File "C:\Anaconda\lib\site-packages\pymongo\collection.py", line 2348, in __call__

self.__name.split(".")[-1])


TypeError: 'Collection' object is not callable. If you meant to call the 'fine_one' method on a 'Collection' object it is failing because no such method exists.



In [20]: print aapl_collection.find_one({"time",dt.datetime(2014,11,17,10,0,0)})

Traceback (most recent call last):


File "<ipython-input-20-3078cc9dd8f6>", line 1, in <module>

print aapl_collection.find_one({"time",dt.datetime(2014,11,17,10,0,0)})


File "C:\Anaconda\lib\site-packages\pymongo\collection.py", line 1006, in find_one

for result in cursor.limit(-1):


File "C:\Anaconda\lib\site-packages\pymongo\cursor.py", line 1097, in next

if len(self.__data) or self._refresh():


File "C:\Anaconda\lib\site-packages\pymongo\cursor.py", line 1019, in _refresh

self.__read_concern))


File "C:\Anaconda\lib\site-packages\pymongo\cursor.py", line 850, in __send_message

**kwargs)


File "C:\Anaconda\lib\site-packages\pymongo\mongo_client.py", line 794, in _send_message_with_response

exhaust)


File "C:\Anaconda\lib\site-packages\pymongo\mongo_client.py", line 805, in _reset_on_error

return func(*args, **kwargs)


File "C:\Anaconda\lib\site-packages\pymongo\server.py", line 108, in send_message_with_response

set_slave_okay, sock_info.is_mongos, use_find_cmd)


File "C:\Anaconda\lib\site-packages\pymongo\message.py", line 275, in get_message

spec, self.fields, self.codec_options)


InvalidDocument: Cannot encode object: set(['time', datetime.datetime(2014, 11, 17, 10, 0)])



In [21]: print aapl_collection.find_one({"time",dt.datetime(2014,10,17,10,0,0)})

Traceback (most recent call last):


File "<ipython-input-21-bad9bd1fe5ee>", line 1, in <module>

print aapl_collection.find_one({"time",dt.datetime(2014,10,17,10,0,0)})


File "C:\Anaconda\lib\site-packages\pymongo\collection.py", line 1006, in find_one

for result in cursor.limit(-1):


File "C:\Anaconda\lib\site-packages\pymongo\cursor.py", line 1097, in next

if len(self.__data) or self._refresh():


File "C:\Anaconda\lib\site-packages\pymongo\cursor.py", line 1019, in _refresh

self.__read_concern))


File "C:\Anaconda\lib\site-packages\pymongo\cursor.py", line 850, in __send_message

**kwargs)


File "C:\Anaconda\lib\site-packages\pymongo\mongo_client.py", line 794, in _send_message_with_response

exhaust)


File "C:\Anaconda\lib\site-packages\pymongo\mongo_client.py", line 805, in _reset_on_error

return func(*args, **kwargs)


File "C:\Anaconda\lib\site-packages\pymongo\server.py", line 108, in send_message_with_response

set_slave_okay, sock_info.is_mongos, use_find_cmd)


File "C:\Anaconda\lib\site-packages\pymongo\message.py", line 275, in get_message

spec, self.fields, self.codec_options)


InvalidDocument: Cannot encode object: set([datetime.datetime(2014, 10, 17, 10, 0), 'time'])



In [22]: print aapl_collection.find_one({"time":dt.datetime(2014,10,17,10,0,0)})

None


In [23]: print aapl_collection.find_one({"time":dt.datetime(2014,11,17,10,0,0)})

{u'last': 114.96, u'vol': 1900000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 0), u'_id': ObjectId('5687c5bcc589e503c48f1b26'), u'ticker': u'aapl'}


In [24]: from bson.objectid import ObjectId


In [25]: print aapl_collection.find_one({"_id":ObjectId("5687c5bcc589e503c48f1b26")})

{u'last': 114.96, u'vol': 1900000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 0), u'_id': ObjectId('5687c5bcc589e503c48f1b26'), u'ticker': u'aapl'}


In [26]: aapl_collection.insert([tick,

    ...: {"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,1,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":114.49,

    ...: "last":115.00

    ...: "vol":2000000},

    ...: {"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,2,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":113.49,

    ...: "last":115.00

    ...: "vol":2100000}])

File "<ipython-input-26-7ed8e13fedcc>", line 8

"vol":2000000},

^

SyntaxError: invalid syntax



In [27]: aapl_collection.insert([tick,

    ...: {"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,1,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":114.49,

    ...: "last":115.00,

    ...: "vol":2000000},

    ...: {"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,2,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":113.49,

    ...: "last":115.00,

    ...: "vol":2100000}])

Traceback (most recent call last):


File "<ipython-input-27-6a7b58a62342>", line 15, in <module>

"vol":2100000}])


File "C:\Anaconda\lib\site-packages\pymongo\collection.py", line 2200, in insert

check_keys, manipulate, write_concern)


File "C:\Anaconda\lib\site-packages\pymongo\collection.py", line 579, in _insert

_check_write_command_response(results)


File "C:\Anaconda\lib\site-packages\pymongo\helpers.py", line 300, in _check_write_command_response

raise DuplicateKeyError(error.get("errmsg"), 11000, error)


DuplicateKeyError: E11000 duplicate key error collection: ticks-db.aapl-collection index: _id_ dup key: { : ObjectId('5688e2a7c589e52730b2e517') }



In [28]: aapl_collection.remove()

Out[28]: {u'n': 4, u'ok': 1}


In [29]: aapl_collection.insert([tick,

    ...: {"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,1,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":114.49,

    ...: "last":115.00,

    ...: "vol":2000000},

    ...: {"ticker":"aapl",

    ...: "time":dt.datetime(2014,11,17,10,2,0),

    ...: "open":115.58,

    ...: "high":116.08,

    ...: "low":113.49,

    ...: "last":115.00,

    ...: "vol":2100000}])

Out[29]:

[ObjectId('5688e2a7c589e52730b2e517'),

ObjectId('5688e4e9c589e52730b2e51a'),

ObjectId('5688e4e9c589e52730b2e51b')]


In [30]: print aapl_collection.count()

3


In [31]: print aapl_collection.find({"open":115.58}).count()

3


In [32]: for aapl_tick in aapl_collection.find():

    ...: print aapl_tick

    ...:

{u'last': 114.96, u'vol': 1900000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 0), u'_id': ObjectId('5688e2a7c589e52730b2e517'), u'ticker': u'aapl'}

{u'last': 115.0, u'vol': 2000000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 1), u'_id': ObjectId('5688e4e9c589e52730b2e51a'), u'ticker': u'aapl'}

{u'last': 115.0, u'vol': 2100000, u'open': 115.58, u'high': 116.08, u'low': 113.49, u'time': datetime.datetime(2014, 11, 17, 10, 2), u'_id': ObjectId('5688e4e9c589e52730b2e51b'), u'ticker': u'aapl'}


In [33]: cutoff_time=dt.datetime(2014,11,17,10,2,0)


In [34]: for tick in aapl_collection.find({"time":{"$lt":cutoff_time}}).sort("time"):

    ...: print tick

    ...:

{u'last': 114.96, u'vol': 1900000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 0), u'_id': ObjectId('5688e2a7c589e52730b2e517'), u'ticker': u'aapl'}

{u'last': 115.0, u'vol': 2000000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 1), u'_id': ObjectId('5688e4e9c589e52730b2e51a'), u'ticker': u'aapl'}


In [35]: sorted_ticks=aapl_collection.find().sort([("time",pymongo.DESCENDING)])


In [36]: for tick in sorted_ticks:

    ...: print tick

    ...:

{u'last': 115.0, u'vol': 2100000, u'open': 115.58, u'high': 116.08, u'low': 113.49, u'time': datetime.datetime(2014, 11, 17, 10, 2), u'_id': ObjectId('5688e4e9c589e52730b2e51b'), u'ticker': u'aapl'}

{u'last': 115.0, u'vol': 2000000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 1), u'_id': ObjectId('5688e4e9c589e52730b2e51a'), u'ticker': u'aapl'}

{u'last': 114.96, u'vol': 1900000, u'open': 115.58, u'high': 116.08, u'low': 114.49, u'time': datetime.datetime(2014, 11, 17, 10, 0), u'_id': ObjectId('5688e2a7c589e52730b2e517'), u'ticker': u'aapl'}


In [37]: