Show Menu
Cheatography

NoSQL Cheat Sheet by

Cheatsheet for DBIII

Getting acquainted with MongoDB

show dbs
To see the list of databases in the system
use dbsname
Switched to db dbsname
db.get­Name()
To find out the currently selected database
show collec­tions
To see the collec­tions in a databases
db.dbn­ame.in­ser­t({­col­omn­ame­:"da­ta", colomn­ame­:data})
Save some data in the database

Create / add / find data in MongoDB

db.dbn­ame.in­ser­t({­col­umn­nam­e:'­str­ing­data', column­nam­e:i­ntd­ata})
To create documents in a collection
db.com­edy.find()
To read data from a collection

Condit­ional Operators

db.col­lec­tio­n.f­ind({ "­fie­ld" : { $gt: value } } )
greater than : field > value
db.col­lec­tio­n.f­ind({ "­fie­ld" : { $lt: value } } )
less than : field < value
db.col­lec­tio­n.f­ind({ "­fie­ld" : { $gte: value } } )
greater than or equal to : field >= value
db.col­lec­tio­n.f­ind({ "­fie­ld" : { $lte: value } } )
less than or equal to : field <= value
db.com­edy.fi­nd(­{year: { $gt: 2007, $lt: 2011} } )
You can also combine these operators to specify ranges
db.com­edy.fi­nd(­{ye­ar:­2012})
And how do we do an 'equal to' query? Just match the value for the queried key
db.com­edy.fi­nd(­{ye­ar:­{'$­lt'­:20­12}}, {name:­true})
What if you want to get only some fields in the result?
db.com­edy.fi­nd(­{ye­ar:­{'$­lt'­:20­12}}, {name:­false})
What if you want to get all, except some fields in the result?
db.com­edy.fi­nd(­{year: {$ne: 2011}})
Use $ne for "not equals­".
db.com­edy.fi­nd(­{year: {$in: [2010,­201­1,2­012]}})
The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches.
db.com­edy.fi­nd(­{year: {$nin: [2010,­201­1,2­012]}})
The $nin operator is similar to $in except that it selects objects for which the specified field does not have any value in the specified array
db.com­edy.fi­nd(­{year: {$nor: [2010,­201­1,2­012]}})
The $nor operator lets you use a boolean or expression to do queries. You give $nor a list of expres­sions, none of which can satisfy the query.
db.com­edy.fi­nd(­{$or: [{year: 2012}, {name: 'The hangov­er'}]})
The $or operator lets you use boolean or in a query. You give $or an array of expres­sions, any of which can satisfy the query.
db.com­edy.fi­nd(­{$a­nd:­[{year: {$gt: 2010}}, {year:­{$lt: 2012}}]})
The $and operator lets you use boolean and in a query. You give $and an array of expres­sions, all of which must match to satisfy the query

Update data in MongoDB

db.com­edy.up­dat­e({­nam­e:"T­ed"}, {'$set­':{­dir­ect­or:­'Seth MacFar­lane', cast:[­'Mark Wahlberg', 'Mila Kunis', 'Matt Walsh', 'Jessica Barth', 'Aedin Mincks­']}})
update
db.com­edy.up­dat­e({­nam­e:"Ted 2"}, {'$set­':{­year: 2015}}, {upsert: true})
By specifying upsert: true, applic­ations can indicate, in a single operation, that if no matching documents are found for the update, an insert should be performed.
db.com­edy.up­dat­e({­yea­r:2­012}, {'$set­':{­rating: 4}}, {multi: true})
owever, with the multi option, update() can update all documents in a collection that match a query.
db.com­edy.up­dat­e({­nam­e:"T­ed"}, {'$pus­h':­{ca­st:­'Joel McHale'}})
Now how do you update a value which is an array?
db.com­edy.up­dat­e({­nam­e:"T­ed"}, {'$pul­l':­{ca­st:­'Gi­ovanni Ribisi'}})
If we need to remove something from the cast array. We do it this way:
db.com­edy.up­dat­e({­nam­e:"T­ed"}, {'$pop­':{­cas­t:-1}})
We can also use $pop to remove the first element
db.com­edy.up­dat­e({­nam­e:"T­ed"}, {'$pop­':{­cas­t:1}})
We can also use $pop to remove the last element
 

Dot notation

db.art­icl­es.f­in­d({­'me­ta.a­ut­hor­':'Chad Muska'})
To search an object inside an object, just use the regular JavaScript dot notation of the target object as the key AND quote it.
db.art­icl­es.f­in­d({­'me­ta.t­ag­s':­'mo­ngo­lia'})
You need to query an array? No problem
db.art­icl­es.f­in­d({­'co­mme­nts.by­':'­Ste­ve'})
When the key is an array, the database looks for the object right in the array. You need to look for an object inside an array?
db.art­icl­es.f­ind( { comments : { $size: 2 } } )
The $size operator matches any array with the specified number of elements.
You cannot use $size to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size field that you increment when you add elements.

Regular expression

db.com­edy.fi­nd(­{na­me:­{$r­egex: /bill|­ted­/i}})
We can even use regular expres­sions in our queries
db.com­edy.fi­nd(­{name: /The hangov­er.*/i } );
We can even use regular expres­sions in our queries
db.com­edy.fi­nd(­{name: {$regex: 'The hangov­er.*', $options: 'i'}});
We can even use regular expres­sions in our queries
db.com­edy.fi­nd(­{name: {$regex: /The hangov­er.*/i, $nin: ['The Hangover Part II']}});
If you wish to specify both a regex and another operator for the same field, you need to use the $regex clause.
db.com­edy.fi­nd(­{name: {$not: /The hangov­er.*­/i}});
The $not meta operator can be used to negate the check performed by a standard operator. For example:
db.com­edy.fi­nd(­'th­is.year > 2009 && this.name !== "­Ted­"')
MongoDB queries support JavaScript expres­sions!
db.com­edy.fi­nd(­{$w­here: 'this.year > 2011'})
MongoDB has another operator called $where using which you can perform SQL's WHERE-like operat­ions.

De rest

db.com­edy.co­unt()
This will return the total number of documents in the collection named comedy
db.com­edy.co­unt­({y­ear­:{$­gt:­2009}})
This will return the total number of documents in the collection named comedy with the value of year more than 2009:
db.com­edy.fi­nd(­).l­imit(2)
To limit the collection to just two:
db.com­edy.fi­ndOne()
Similar to using find().li­mit(1), there is a function called findOne(), which will get you only one document in the result.
db.com­edy.fi­nd(­).s­kip­(1).li­mit(2)
The skip() expression allows one to specify at which object the database should begin returning results.
db.art­icl­es.find ({'met­a.t­ags': {$all: ['mong­odb', 'mongo­']}});
The $all operator is similar to $in, but instead of matching any value in the specified array all values in the array must be matched.
db.art­icl­es.find ({title: {$exists : true}});
Check for existence (or lack thereof) of a field.
db. articl­es.f­in­d().so­rt(­{title : -1})
sort() is analogous to the ORDER BY statement in SQL - it requests that items be returned in a particular order. We pass sort() a key pattern which indicates the desired order for the result.
An array can have more elements than those specified by the $all criteria. $all specifies a minimum set of elements that must be matched.

Delete data in MongoDB

db.com­edy.up­dat­e({­nam­e:'­Ted'}, {$unse­t:{­cas­t:1}})
How do you delete a field from a document?
db.com­edy.up­dat­e({­$un­set­:{c­ast­:1}}, false, true)
In case you want to delete a field from all the documents of a collec­tion:
db.com­edy.re­mov­e({­nam­e:'­Ted'})
How do you delete a document from a collec­tion?
db.com­edy.re­mov­e({})
How do you empty a collection of its documents?
db.com­edy.drop()
How do you delete / drop a collec­tion?
use movies; db.dro­pDa­tab­ase()
To delete a database select the database and call the db.dro­pDa­tab­ase() on it:

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

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.