Skip to content

Instantly share code, notes, and snippets.

@matteocollina
Last active June 12, 2018 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteocollina/9d923e08fe63dc7084b7597c7b7d56a5 to your computer and use it in GitHub Desktop.
Save matteocollina/9d923e08fe63dc7084b7597c7b7d56a5 to your computer and use it in GitHub Desktop.
Lesson on Mongo db @ University (Milan). From install packages on python, to test Pymongo on macOSX
# ------ Prerequisites ------
# Start server
$ mongod
# Start client
$ mongo
# Install packages on python 2.7 (required version of "pip" >= 10)
$ sudo pip install --ignore-installed pymongo
$ sudo pip install --ignore-installed numpy
$ sudo pip install --ignore-installed pandas
# Install pandas in python 3 (I use 3.6)
$ sudo python3 -m pip install pandas
$ sudo python3 -m pip show pandas
# If I query a collection that does not exist is created.
# To show dbs:
$ show dbs
# The client contains a variable (db) that is initialized with the current db
$ use [dbname]
# I see the list of db (as long as I do not write anything is virtual)
$ db
# See the collections inside that db
$ show collections
# Returns the data of the collection
$ db.hello.find()
# Delete the collection
$ db.hello.drop()
# Delete db
$ db.dropDatabase()
# Collection = set of json objects
# There are no data constraints ({"age": "18"}, {"age": 18}), there are two
# objects in the same collection with different values for the same key => more flexible.
# a key can have numeric values, strings, bool, but also arrays (ordered list of objects
# without constraint of what these objects are)
# In MongoDb it is listed inside the object, the list of other objects to which it is connected. Joints are almost never made
# because the data is all in the same collection.
# If the key is missing in an object, it is as if it were null
# To create a JSON file directly from the database, use mongoexport
# [go to current dir]
$ mongoexport --db [db_name] --collection [collection_name] --jsonArray --out [json_name].json
# ------ AGGREGATION FRAMEWORK ------
# Aggregation framework => aggregate: transform data,
# via a pipeline of operations, which must be compatible with each other
# the i.thest must be compatible with the i-1.exima.
# The output could be a: 1.cursor, 2.new collection mongo db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment