{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import pymongo\n", "client = pymongo.MongoClient('mongodb://ogedei:27017/')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Connect to the `accidents` database" ] }, { "cell_type": "code", "collapsed": false, "input": [ "db = client.accidents\n", "accidents = db.accidents" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "accidents.find().count()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "1355615" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Drop any existing small accident database" ] }, { "cell_type": "code", "collapsed": false, "input": [ "client.drop_database('asmall')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a new database with a new collection.\n", "\n", "Note that this is created lazily, so neither the database nor the collection will appear on the server until we've put some data in it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "small_db = client.asmall\n", "small_accidents = small_db.accidents" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy the first 100 accidents across." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for a in accidents.find(limit=100):\n", " small_accidents.insert(a)\n", "small_accidents.find().count()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "100" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the index for it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "small_accidents.create_index('Accident_Index')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "'Accident_Index_1'" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }