Cheatography
https://cheatography.com
MongoDB Basic - CLI
|
Start the Database |
|
Start the mongo command line
|
|
Show the databases |
|
Use the database named db
|
|
Display the current database collections
|
Finds - db.collection
|
Displays documents from collection
(first 10) |
|
Iterate the last command |
.find( query [,fields] )
|
Find one document by conditions |
.find().limit(n:number)
|
Find n documents |
|
Format results in Mongo Shell |
.find().sort(key:1|-1)
|
Sort by key in ascending (1) or descending (-1) order |
|
Skip 5 documents (similar to offset) |
Modify - db.collection
.insert(document(s) [,options])
|
Insert a new document or multiple documents(if provided an array of documents) in the collection. Options: writeConcern, ordered |
.insertOne(document, [,options])
|
.insertMany(array of documents, [,options])
|
.update(query, update [,options])
|
Update the documents matched by the query. Options: upsert(insert if no match), multi(aply to multiple elements), writeConcern |
.remove(query [,options])
|
Remove some documents from a collection. {} for all. options: { justOne, writeConcern } |
|
Same as above |
|
Same as above |
|
|
Mongoengine - Python
from mongoengine import *
|
|
Connect to a database |
class MyDocument(Document)
|
Define a Document |
class MyDocument(EmbeddedDocument)
|
Define a Document |
obj = MyDocument(field=value)
|
Instantiate a Document |
|
Save the new Document |
--------------------------- Fields --------------------------- |
|
|
ReferenceField(document)
|
|
EmbeddedDocumentField(document_class)
|
|
--------------- Field Args --------------- |
|
Optional Argument |
|
Optional Argument |
|
Optional Argument |
meta = {'allow_inheritance': True}
|
reverse_delete_rule=CASCADE
|
--------------------------- Querying --------------------------- |
|
List of documents |
MyDocument.objects(key=value)
|
List of Documents with query |
MyDocument.objects.count()
|
Count Documents |
MyDocument.objects(key__op=value)
|
Query key
with operator __op
|
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by amicheletti