Cheatography
https://cheatography.com
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 collections
|
Display the current database collections |
Finds - db.collection
db.collection.find()
|
Displays documents from collection (first 10) |
it
|
Iterate the last command |
.find( query [,fields] )
|
Find one document by conditions |
.find().limit(n:number)
|
Find n documents |
.find().pretty()
|
Format results in Mongo Shell |
.find().sort(key:1|-1)
|
Sort by key in ascending (1) or descending (-1) order |
.find().skip(5)
|
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 } |
.deleteOne(query)
|
Same as above |
deleteMany(query)
|
Same as above |
|
|
Mongoengine - Python
from mongoengine import *
|
connect('tumblelog')
|
Connect to a database |
class MyDocument(Document)
|
Define a Document |
class MyDocument(EmbeddedDocument)
|
Define a Document |
obj = MyDocument(field=value)
|
Instantiate a Document |
obj.save()
|
Save the new Document |
--------------------------- Fields --------------------------- |
StringField
|
ListField(type)
|
ReferenceField(document)
|
FloatField
|
EmbeddedDocumentField(document_class)
|
|
--------------- Field Args --------------- |
required=True
|
Optional Argument |
max_length=120
|
Optional Argument |
primary_key=True
|
Optional Argument |
meta = {'allow_inheritance': True}
|
reverse_delete_rule=CASCADE
|
--------------------------- Querying --------------------------- |
MyDocument.objects
|
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