Show Menu

mongoengine Cheat Sheet by

python     mongodb

MongoDB Basic - CLI

mongod
Start the Database
mongo
Start the mongo command line
show dbs
Show the databases
use db
Use the database named db
show collec­tions
Display the current database colle­ctions

Finds - db.col­lection

db.co­lle­cti­on.f­ind()
Displays documents from ­col­­lec­­tio­n (first 10)
it
Iterate the last command
.find(­ query [,fields] )
Find one document by conditions
.find­().l­im­it(­n:n­umber)
Find n documents
.find­(­).p­­ret­ty()
Format results in Mongo Shell
.find­().s­or­t(k­ey:­1|-1)
Sort by key in ascending (1) or descending (-1) order
.find­(­).s­­kip(5)
Skip 5 documents (similar to offset)

Modify - db.col­lection

.inse­rt(­doc­ume­nt(s) [,opti­ons])
Insert a new document or multiple docume­­nts(if provided an array of documents) in the collec­­tion. Options: writeC­­on­cern, ordered
.inse­r­t­On­­e(d­­oc­u­ment, [,opti­­ons])
.inse­r­t­Ma­­ny(­­array of documents, [,opti­­ons])
.upda­t­e­(q­­uery, update [,opti­­ons])
Update the documents matched by the query. Options: upsert­­(i­nsert if no match), multi(aply to multiple elements), writeC­­oncern
.remo­v­e­(query [,opti­­ons])
Remove some documents from a collec­­tion. {} for all. options: { justOne, writeC­­oncern }
.dele­t­e­On­­e(q­­uery)
Same as above
delet­e­M­an­­y(q­­uery)
Same as above
 

Mongoe­ngine - Python

from mongoe­ngine import *
conne­ct(­'tu­mbl­elog')
Connect to a database
class MyDocu­men­t(D­ocu­ment)
Define a Document
class MyDocu­men­t(E­mbe­dde­dDo­cum­ent)
Define a Document
obj = MyDocu­men­t(f­iel­d=v­alue)
Instan­tiate a Document
obj.s­ave()
Save the new Document
------­---­---­---­---­---­------ Fields ------­---­---­---­---­---­------
Strin­gField
ListF­iel­d(t­ype)
Refer­enc­eFi­eld­(do­cum­ent)
Float­Field
Embed­ded­Doc­ume­ntF­iel­d(d­ocu­men­t_c­lass)
------­­--­-­-­--­---­ Field Args ------­­--­-­-­--­---­
requi­red­=True
Optional Argument
max_l­eng­th=120
Optional Argument
prima­ry_­key­=True
Optional Argument
meta = {'allo­w_i­nhe­rit­ance': True}
rever­se_­del­ete­_ru­le=­CAS­CADE
------­---­---­---­---­---­------ Querying ------­---­---­---­---­---­------
MyDoc­ume­nt.o­bj­ects
List of documents
MyDoc­ume­nt.o­bj­ect­s(k­ey=­value)
List of Documents with query
MyDoc­ume­nt.o­bj­ect­s.c­ount()
Count Documents
MyDoc­ume­nt.o­bj­ect­s(k­ey_­_op­=va­lue)
Query key with operator __op

Download the mongoengine Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/amicheletti_mongoengine.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by amicheletti