\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Xplendit} \pdfinfo{ /Title (nosql.pdf) /Creator (Cheatography) /Author (Xplendit) /Subject (NoSQL Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{052AA3} \definecolor{LightBackground}{HTML}{EFF1F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{NoSQL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Xplendit} via \textcolor{DarkBackground}{\uline{cheatography.com/25193/cs/6504/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Xplendit \\ \uline{cheatography.com/xplendit} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 26th December, 2015.\\ Updated 10th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Getting acquainted with MongoDB}} \tn % Row 0 \SetRowColor{LightBackground} show dbs & To see the list of databases in the system \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} use dbsname & Switched to db dbsname \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} db.getName() & To find out the currently selected database \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} show collections & To see the collections in a databases \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} db.dbname.insert(\{colomname:"data", colomname:data\}) & Save some data in the database \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Create / add / find data in MongoDB}} \tn % Row 0 \SetRowColor{LightBackground} db.dbname.insert(\{columnname:'stringdata', columnname:intdata\}) & To create documents in a collection \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} db.comedy.find() & To read data from a collection \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Conditional Operators}} \tn % Row 0 \SetRowColor{LightBackground} db.collection.find(\{ "field" : \{ \$gt: value \} \} ) & greater than : field \textgreater{} value \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} db.collection.find(\{ "field" : \{ \$lt: value \} \} ) & less than : field \textless{} value \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} db.collection.find(\{ "field" : \{ \$gte: value \} \} ) & greater than or equal to : field \textgreater{}= value \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} db.collection.find(\{ "field" : \{ \$lte: value \} \} ) & less than or equal to : field \textless{}= value \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} db.comedy.find(\{year: \{ \$gt: 2007, \$lt: 2011\} \} ) & You can also combine these operators to specify ranges \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} db.comedy.find(\{year:2012\}) & And how do we do an 'equal to' query? Just match the value for the queried key \tn % Row Count 19 (+ 4) % Row 6 \SetRowColor{LightBackground} db.comedy.find(\{year:\{'\$lt':2012\}\}, \{name:true\}) & What if you want to get only some fields in the result? \tn % Row Count 22 (+ 3) % Row 7 \SetRowColor{white} db.comedy.find(\{year:\{'\$lt':2012\}\}, \{name:false\}) & What if you want to get all, except some fields in the result? \tn % Row Count 26 (+ 4) % Row 8 \SetRowColor{LightBackground} db.comedy.find(\{year: \{\$ne: 2011\}\}) & Use \$ne for "not equals". \tn % Row Count 28 (+ 2) % Row 9 \SetRowColor{white} db.comedy.find(\{year: \{\$in: {[}2010,2011,2012{]}\}\}) & The \$in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches. \tn % Row Count 34 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Conditional Operators (cont)}} \tn % Row 10 \SetRowColor{LightBackground} db.comedy.find(\{year: \{\$nin: {[}2010,2011,2012{]}\}\}) & 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 \tn % Row Count 8 (+ 8) % Row 11 \SetRowColor{white} db.comedy.find(\{year: \{\$nor: {[}2010,2011,2012{]}\}\}) & The \$nor operator lets you use a boolean or expression to do queries. You give \$nor a list of expressions, none of which can satisfy the query. \tn % Row Count 16 (+ 8) % Row 12 \SetRowColor{LightBackground} db.comedy.find(\{\$or: {[}\{year: 2012\}, \{name: 'The hangover'\}{]}\}) & The \$or operator lets you use boolean or in a query. You give \$or an array of expressions, any of which can satisfy the query. \tn % Row Count 23 (+ 7) % Row 13 \SetRowColor{white} db.comedy.find(\{\$and:{[}\{year: \{\$gt: 2010\}\}, \{year:\{\$lt: 2012\}\}{]}\}) & The \$and operator lets you use boolean and in a query. You give \$and an array of expressions, all of which must match to satisfy the query \tn % Row Count 30 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Update data in MongoDB}} \tn % Row 0 \SetRowColor{LightBackground} db.comedy.update(\{name:"Ted"\}, \{'\$set':\{director:'Seth MacFarlane', cast:{[}'Mark Wahlberg', 'Mila Kunis', 'Matt Walsh', 'Jessica Barth', 'Aedin Mincks'{]}\}\}) & update \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} db.comedy.update(\{name:"Ted 2"\}, \{'\$set':\{year: 2015\}\}, \{upsert: true\}) & By specifying upsert: true, applications can indicate, in a single operation, that if no matching documents are found for the update, an insert should be performed. \tn % Row Count 17 (+ 9) % Row 2 \SetRowColor{LightBackground} db.comedy.update(\{year:2012\}, \{'\$set':\{rating: 4\}\}, \{multi: true\}) & owever, with the multi option, update() can update all documents in a collection that match a query. \tn % Row Count 22 (+ 5) % Row 3 \SetRowColor{white} db.comedy.update(\{name:"Ted"\}, \{'\$push':\{cast:'Joel McHale'\}\}) & Now how do you update a value which is an array? \tn % Row Count 26 (+ 4) % Row 4 \SetRowColor{LightBackground} db.comedy.update(\{name:"Ted"\}, \{'\$pull':\{cast:'Giovanni Ribisi'\}\}) & If we need to remove something from the cast array. We do it this way: \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Update data in MongoDB (cont)}} \tn % Row 5 \SetRowColor{LightBackground} db.comedy.update(\{name:"Ted"\}, \{'\$pop':\{cast:-1\}\}) & We can also use \$pop to remove the first element \tn % Row Count 3 (+ 3) % Row 6 \SetRowColor{white} db.comedy.update(\{name:"Ted"\}, \{'\$pop':\{cast:1\}\}) & We can also use \$pop to remove the last element \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dot notation}} \tn % Row 0 \SetRowColor{LightBackground} db.articles.find(\{'meta.author':'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. \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} db.articles.find(\{'meta.tags':'mongolia'\}) & You need to query an array? No problem \tn % Row Count 10 (+ 3) % Row 2 \SetRowColor{LightBackground} db.articles.find(\{'comments.by':'Steve'\}) & 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? \tn % Row Count 17 (+ 7) % Row 3 \SetRowColor{white} db.articles.find( \{ comments : \{ \$size: 2 \} \} ) & The \$size operator matches any array with the specified number of elements. \tn % Row Count 21 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{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.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular expression}} \tn % Row 0 \SetRowColor{LightBackground} db.comedy.find(\{name:\{\$regex: /bill|ted/i\}\}) & We can even use regular expressions in our queries \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} db.comedy.find(\{name: /The hangover.*/i \} ); & We can even use regular expressions in our queries \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} db.comedy.find(\{name: \{\$regex: 'The hangover.*', \$options: 'i'\}\}); & We can even use regular expressions in our queries \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} db.comedy.find(\{name: \{\$regex: /The hangover.*/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. \tn % Row Count 16 (+ 6) % Row 4 \SetRowColor{LightBackground} db.comedy.find(\{name: \{\$not: /The hangover.*/i\}\}); & The \$not meta operator can be used to negate the check performed by a standard operator. For example: \tn % Row Count 22 (+ 6) % Row 5 \SetRowColor{white} \seqsplit{db.comedy.find('this.year} \textgreater{} 2009 \&\& this.name !== "Ted"') & MongoDB queries support JavaScript expressions! \tn % Row Count 25 (+ 3) % Row 6 \SetRowColor{LightBackground} db.comedy.find(\{\$where: 'this.year \textgreater{} 2011'\}) & MongoDB has another operator called \$where using which you can perform SQL's WHERE-like operations. \tn % Row Count 30 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{De rest}} \tn % Row 0 \SetRowColor{LightBackground} db.comedy.count() & This will return the total number of documents in the collection named comedy \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} db.comedy.count(\{year:\{\$gt:2009\}\}) & This will return the total number of documents in the collection named comedy with the value of year more than 2009: \tn % Row Count 10 (+ 6) % Row 2 \SetRowColor{LightBackground} \seqsplit{db.comedy.find().limit(2)} & To limit the collection to just two: \tn % Row Count 12 (+ 2) % Row 3 \SetRowColor{white} db.comedy.findOne() & Similar to using find().limit(1), there is a function called findOne(), which will get you only one document in the result. \tn % Row Count 19 (+ 7) % Row 4 \SetRowColor{LightBackground} \seqsplit{db.comedy.find().skip(1).limit(2)} & The skip() expression allows one to specify at which object the database should begin returning results. \tn % Row Count 25 (+ 6) % Row 5 \SetRowColor{white} db.articles.find (\{'meta.tags': \{\$all: {[}'mongodb', '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. \tn % Row Count 32 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{De rest (cont)}} \tn % Row 6 \SetRowColor{LightBackground} db.articles.find (\{title: \{\$exists : true\}\}); & Check for existence (or lack thereof) of a field. \tn % Row Count 3 (+ 3) % Row 7 \SetRowColor{white} db. articles.find().sort(\{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. \tn % Row Count 13 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{An array can have more elements than those specified by the \$all criteria. \$all specifies a minimum set of elements that must be matched.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Delete data in MongoDB}} \tn % Row 0 \SetRowColor{LightBackground} db.comedy.update(\{name:'Ted'\}, \{\$unset:\{cast:1\}\}) & How do you delete a field from a document? \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} db.comedy.update(\{\$unset:\{cast:1\}\}, false, true) & In case you want to delete a field from all the documents of a collection: \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} db.comedy.remove(\{name:'Ted'\}) & How do you delete a document from a collection? \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} db.comedy.remove(\{\}) & How do you empty a collection of its documents? \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} db.comedy.drop() & How do you delete / drop a collection? \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} use movies; db.dropDatabase() & To delete a database select the database and call the db.dropDatabase() on it: \tn % Row Count 19 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}