Show Menu
Cheatography

mongoengine Cheat Sheet by

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
collec­tions

Finds - db.col­lection

db.col­lec­tio­n.f­ind()
Displays documents from
­col­l­ec­­tio­n
(first 10)
it
Iterate the last command
.find(­ query [,fields] )
Find one document by conditions
.find(­).l­imi­t(n­:nu­mber)
Find n documents
.find(­­).p­­r­etty()
Format results in Mongo Shell
.find(­).s­ort­(ke­y:1|-1)
Sort by key in ascending (1) or descending (-1) order
.find(­­).s­­k­ip(5)
Skip 5 documents (similar to offset)

Modify - db.col­lection

.inser­t(d­ocu­ment(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
.inser­­tO­n­e­(d­­ocu­­ment, [,opti­­ons])
.inser­­tM­a­n­y(­­array of documents, [,opti­­ons])
.updat­­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
.remov­­e(­query [,opti­­ons])
Remove some documents from a collec­­tion. {} for all. options: { justOne, writeC­­oncern }
.delet­­eO­n­e­(q­­uery)
Same as above
delete­­Ma­n­y­(q­­uery)
Same as above
 

Mongoe­ngine - Python

from mongoe­ngine import *
connec­t('­tum­ble­log')
Connect to a database
class MyDocu­men­t(D­ocu­ment)
Define a Document
class MyDocu­men­t(E­mbe­dde­dDo­cument)
Define a Document
obj = MyDocu­men­t(f­iel­d=v­alue)
Instan­tiate a Document
obj.save()
Save the new Document
------­---­---­---­---­---­------ Fields ------­---­---­---­---­---­------
String­Field
ListFi­eld­(type)
Refere­nce­Fie­ld(­doc­ument)
FloatField
Embedd­edD­ocu­men­tFi­eld­(do­cum­ent­_class)
------­­--­-­-­--­---­ Field Args ------­­--­-­-­--­---­
requir­ed=True
Optional Argument
max_le­ngt­h=120
Optional Argument
primar­y_k­ey=True
Optional Argument
meta = {'allo­w_i­nhe­rit­ance': True}
revers­e_d­ele­te_­rul­e=C­ASCADE
------­---­---­---­---­---­------ Querying ------­---­---­---­---­---­------
MyDocu­men­t.o­bjects
List of documents
MyDocu­men­t.o­bje­cts­(ke­y=v­alue)
List of Documents with query
MyDocu­men­t.o­bje­cts.co­unt()
Count Documents
MyDocu­men­t.o­bje­cts­(ke­y__­op=­value)
Query
key
with operator
__op
 

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.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter

          More Cheat Sheets by amicheletti