Mongodb - Cheat Sheet

ADVERTISEMENT

More information can be found from the MongoDB Tutorial
MongoDB - Cheat Sheet
@
th
Version 1.0 / 27
December 2012 / Page 1 of 4
About this Cheat Sheet
Basic Information
The idea behind this is to have all (well, most) information from the above
Download MongoDB
mentioned Tutorial immediately available in a very compact format. All commands
JSON Specification
can be used on a small data basis created in the insert-section. All information in
BSON Specification
this sheet comes without the slightest warranty for correctness. Use at your own
Java Tutorial
risk. Have fun !
Inserting Documents
db.ships.insert({name:'USS Enterprise-D',operator:'Starfleet',type:'Explorer',class:'Galaxy',crew:750,codes:[10,11,12]})
db.ships.insert({name:'USS Prometheus',operator:'Starfleet',class:'Prometheus',crew:4,codes:[1,14,17]})
db.ships.insert({name:'USS Defiant',operator:'Starfleet',class:'Defiant',crew:50,codes:[10,17,19]})
db.ships.insert({name:'IKS Buruk',operator:' Klingon Empire',class:'Warship',crew:40,codes:[100,110,120]})
db.ships.insert({name:'IKS Somraw',operator:' Klingon Empire',class:'Raptor',crew:50,codes:[101,111,120]})
db.ships.insert({name:'Scimitar',operator:'Romulan Star Empire',type:'Warbird',class:'Warbird',crew:25,codes:[201,211,220]})
db.ships.insert({name:'Narada',operator:'Romulan Star Empire',type:'Warbird',class:'Warbird',crew:65,codes:[251,251,220]})
Finding Documents
Basic Concepts & Shell Commands
db – implicit handle to the used database
db.ships.findOne()
db.ships.<command>
Finds one arbitrary document
ships – name of the used collection
Finds all documents and using nice
Switch to another database
db.ships.find().prettyPrint()
use <database>
formatting
Shows only the names of the ships
Lists the available collections
db.ships.find({}, {name:true, _id:false})
show collections
db.ships.findOne({'name':'USS Defiant'})
Finds one document by attribute
Prints available commands and help
help
Finding Documents using Operators
BSON Types
greater than / greater than equals
String
2
$gt / $gte
db.ships.find({class:{$gt:’P'}
lesser than / lesser than equals
Array
4
$lt / $lte
db.ships.find({class:{$lte:’P'}
does an attribute exist or not
Binary Data
5
$exists
db.ships.find({type:{$exists:true}})
Perl-style pattern matching
Date
9
$regex
db.ships.find({name:{$regex:’^USS\\sE’}})
search by type of an element
$type
db.ships.find({name : {$type:2}})

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4