Last active
December 15, 2015 01:58
-
-
Save marioplumbarius/5183321 to your computer and use it in GitHub Desktop.
Pieces of code I've written while taking 10gen's M101J MongoDB for Java Developers course.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// querying and cursors | |
cursor = db.people.find(); null ; | |
cursor.hasNext() // returns true (if there is another set of results) and false (if not) | |
cursor.next() // returns the next result and move the cursor foward | |
// Limiting the set of results returned by the queries | |
cursor.limit(5); null; // limit the results returned by the cursor (default is 20) | |
// note: the 'null' keyword is used to prevent the mongoshell from printing that query out | |
// Sorting | |
cursor.sort( { key : [1, -1] } ); null; // returns sorted results | |
cursor.sort( { name : -1 } ); null; // returns sorted results in reverse order | |
// Limiting, sorting and skiping | |
cursor.sort( { name : -1 } ).limit(3).skip(2); null; | |
db.scores.find( {type: "exam"} ).sort( { score : -1 } ).skip(50).limit(20); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Inserting, updating, selecting and deleting data from MongoDB | |
*/ | |
// INSERT | |
// fruits collection | |
db.fruits.insert( {name : "apple", color : "red", shape : "round", units : 10} ) | |
db.fruits.insert( {name : "orange", color : "orange", shape : "round", units : 5} ) | |
db.fruits.insert( {name : "banana", color : "yellow"} ) | |
db.fruits.insert( {name : "pineapple"} ) | |
db.fruits.insert( {name : 22} ) | |
db.fruits.insert( {name: "" } ) | |
// people collection | |
db.people.insert( { name : "Mario", hobbies : ["learn new programming languages", "play soccer", "sing"] } ) | |
db.people.insert( { name : "Luan", hobbies : ["skating", "meet new people", "play soccer"] } ) | |
db.people.insert( { name : "John", hobbies : ["skating", "play soccer"] } ) | |
db.people.insert( { name : "Robert", hobbies : ["skating", "sing"] } ) | |
// users collection | |
db.users.insert( { name: "David", email : { work : "[email protected]", personal : "[email protected]" } } ) | |
db.users.insert( { name: "Norris", email : [ {work : "[email protected]", personal : "[email protected]"} ] } ) | |
// SELECT | |
// findOne | |
db.fruits.findOne( {key : "value"}, {key : boolean} ) | |
db.fruits.findOne( {name : "apple"}, {name : true, _id : false} ) | |
// find | |
db.fruits.find() | |
db.fruits.find( {color : "red"}, {_id : false, name : true} ) | |
// greater than, less than and/or equals ($gt, $lt, $gte, $lte) | |
db.fruits.find( { units : { $gt : 5 } } ) | |
db.fruits.find( { units : { $gt : 5, $lte : 8 } } ) | |
// $exists : returns only records containing specifics keys | |
db.fruits.find( { shape : { $exists : true } } ) | |
// $type : returns records matching selected data type | |
db.fruits.find( { name : { $type : 2 } ) // 2 = BSON string type | |
// regex : returns records matching regular expression | |
db.fruits.find( { name : { $regex : "e$" } } ) // returns fruits whose name ends with letter 'e' | |
db.fruits.find( { color : { $regex : "a" } } ) // returns fruits whose color contains letter 'a' | |
db.fruits.find( { color : { $regex : "^y" } } ) // returns fruits whose name begins with the letter 'y' | |
// $or : takes an array | |
db.fruits.find( { $or : [ { name : { $regex : "a" } }, { shape : { $exists : true } } ] } ) | |
db.fruits.find( { $or : [ { units : { $lt : 20 } }, { score : { $gt : 5 } } ] } ) | |
// $and : takes an array | |
db.fruits.find( { $and : [ { name : { $regex : "a"} }, { shape : "round" } ] } ) | |
// querying inside arrays | |
// searching for single values | |
db.people.find( { hobbies : "play soccer" } ) | |
// $all = stands for AND operand | |
db.people.find( { favorites : { $all : [ "play soccer", "skating" ] } } ) | |
// $in = stands for OR operand | |
db.people.find( { favorites : { $in : [ "play soccer", "skating" ] } } ) | |
db.people.find( { name : { $in : [ "Luan", "Mario" ] } } ) | |
// dot notation = search nested documents | |
db.users.find( { "email.work" : "[email protected]" } ) // search inside a nested array field | |
db.users.find( { "email.work" : "[email protected]" } ) // search inside a nested object field |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment