Skip to content

Instantly share code, notes, and snippets.

@1UC1F3R616
Last active April 9, 2020 12:37
Show Gist options
  • Save 1UC1F3R616/3df8a2c4e0c0ca10a8249bdbdd54942b to your computer and use it in GitHub Desktop.
Save 1UC1F3R616/3df8a2c4e0c0ca10a8249bdbdd54942b to your computer and use it in GitHub Desktop.

Install for Debain from here

PyMongo Tutorial (not official)

Python Mongo ODM - MongoEngine

Mongo with Python - Good Examples and doc

Understanding MongoDB

  • Database
    • Collection/s
      • Document/s
  • Collections do not enforce a schema
  • Document has dynamic schema
  • Document is a set of Key Value pair
RDBMS MongoDB
Database Database
Table Collection
Row Document
Column Field
Table Join Embedded Documents
Primary Key Default key_id is given by MongoDB

Creating a Database

# if not present then created else selected
use databasename
# list current db name
db
# list all database
show dbs
# Adding a document in current db
db.movie.insert({
"name":"xyz"
})

Deleting a Database

use databasename
db.dropDatabase()

Creating a Collection

db.createCollection(name, options) # options is like a document itself, where we tell size etc
# example db.createCollection("mycollection")
# example db.createCollection("mycol", {capped:true, autoIndexId:true, size:6142800, max:10000})

Screenshot from 2020-02-15 17-30-20

showcollections # shows the created collection
# MongoDB automatically creates a collection when we add some document to a not created collection
# example: db/magicalCollection.insert({"name":"I love LP"})

Dropping a database

db.mycollection.drop()

Datatypes in MongoDB

  • String
  • Integer
  • Boolean
  • Double
  • Min/Max keys
  • Arrays
  • Timestamp
  • Object
  • Null
  • Symbol
  • Date
  • Object Id
  • Binary data
  • Code
  • Regular expression

Inserting a document

db.collectionname.insert({"name":"Kush"}) # Inserting a single document
db.collectionname.insert([{...}, {...}, {...}]) # Inserting multiple documents

Querying Documents

db.collectionname.find() # returns everything
db.collectionname.find({"number":9588}) # returns all, and iter is returned not array
db.collectionname.find({"number":9588}).pretty() # A prettier output
db.collectionname.findOne({"number":9588}) # returns only 1

Screenshot from 2020-02-15 17-59-32 Screenshot from 2020-02-15 18-21-30 Screenshot from 2020-02-15 18-02-13 Screenshot from 2020-02-15 18-11-14

Updating, Deleting, Projection, Sorting and Limiting Documents in MongoDB

  • You can use update or save
# Updating
db.mycollection.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Overview'}})
db.mycollection.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Overview'}}, {multi:true}) # Multiple updates#

# Deleting
db.mycollection.remove({'title':'MongoDB Overview'}) # justOne=1 to only delete one

# Projection - retrive only some of the key:value pair, which are marked as 1
db.mycol.find({}, {"title":1, _id:0}) # list all but only title

# Limiting Records
db.mycollection.find().limit(100)

# Sorting Documents - 1 for asc, -1 for dec
db.mycollection.find().sort({'likes':1}) # here likes is a key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment