<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Query MongoDB Database Collection\n",
    "\n",
    "This notebook demonstrates how to:\n",
    "\n",
    "* Connect to a MongoDB instance\n",
    "* List the databases for the instance\n",
    "* List the collections for a database\n",
    "* Query a database collection and convert the result to a `pandas.DataFrame`\n",
    "* Bind a DataFrame to an [jupyter-incubator/declarativewidgets](https://github.com/jupyter-incubator/declarativewidgets) `urth-viz-table` widget to display the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pymongo import MongoClient\n",
    "from bson.objectid import ObjectId\n",
    "from urth.widgets.widget_channels import channel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Connect to a MongoDB instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "client = MongoClient('192.168.99.100', 27017)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List the databases in the instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "client.database_names()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get a reference to a database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db = client.demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List the collections in the database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "db.collection_names()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get a reference to a collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "features = db.client_features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Query collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def query_collection(limit=100):\n",
    "    cursor = customers.find({}).limit(limit)\n",
    "    df = pd.DataFrame(list(cursor))\n",<commit_msg>Fix variable name in demo notebook.<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Query MongoDB Database Collection\n",
    "\n",
    "This notebook demonstrates how to:\n",
    "\n",
    "* Connect to a MongoDB instance\n",
    "* List the databases for the instance\n",
    "* List the collections for a database\n",
    "* Query a database collection and convert the result to a `pandas.DataFrame`\n",
    "* Bind a DataFrame to an [jupyter-incubator/declarativewidgets](https://github.com/jupyter-incubator/declarativewidgets) `urth-viz-table` widget to display the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pymongo import MongoClient\n",
    "from bson.objectid import ObjectId\n",
    "from urth.widgets.widget_channels import channel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Connect to a MongoDB instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "client = MongoClient('192.168.99.100', 27017)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List the databases in the instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "client.database_names()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get a reference to a database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db = client.demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List the collections in the database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "db.collection_names()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get a reference to a collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "features = db.client_features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Query collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def query_collection(limit=100):\n",
    "    cursor = features.find({}).limit(limit)\n",
    "    df = pd.DataFrame(list(cursor))\n",
{'fraction_non_alphanumeric': 0.1908944255242875, 'fraction_numerical': 0.006811256497580211, 'mean_word_length': 2.6069812540400776, 'pattern_counts': {'":': 128, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6530010', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1983, 'n_words': 416}