-
-
Save bliotti/157a1441c6443b7abcbc55ced0038342 to your computer and use it in GitHub Desktop.
Revisions
-
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -596,7 +596,7 @@ db.dropDatabase(); ``` To delete specific fields within a document you have to use an update using the $unset operator. ###Indexes INDEX WHATEVER FIELDS YOU QUERY MOST OFTEN BY. ####EXPLAIN Mehtod Get query specifications like objects found, kind of cursor, number of scanned objects, milliseconds it took to do the query. -
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -596,7 +596,9 @@ db.dropDatabase(); ``` To delete specific fields within a document you have to use an update using the $unset operator. ###Indexes ---INDEX WHATEVER FIELDS YOU QUERY MOST OFTEN BY. ####EXPLAIN Mehtod Get query specifications like objects found, kind of cursor, number of scanned objects, milliseconds it took to do the query. ``` db.links.find({title:'Nettuts+'}).explain(); @@ -626,4 +628,15 @@ When documents don't have the selected field they would still get an index. To a db.links.ensureIndex({ title: 1},{ sparse:true }); ``` ####Compound Indexes ``` db.links.ensureIndex({ title:1, url: 1 }); ``` MongoDB can just use a single index per query ###Get rid of indexes ``` db.links.dropIndex({title_1_url_1}); ``` ###Show indexes ``` db.system.indexes.find(); ``` -
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 18 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -602,12 +602,28 @@ Get query specifications like objects found, kind of cursor, number of scanned o db.links.find({title:'Nettuts+'}).explain(); ``` ####ENSURE INDEX Method To create your own indexes: ``` db.links.ensureIndex({ title: 1}); ``` 1 means to index in ascending order. To see if the index was created: ``` db.system.indexes.find(); ``` To set unique indexes for every document: ``` db.links.ensureIndex({ title: 1},{ unique:true }); ``` If there were multiple documents with the same value ``` db.links.ensureIndex({ title: 1},{ unique:true, dropDups:true}); ``` It would only keep the first one and get rid to any subsequent documents with the same field value. When documents don't have the selected field they would still get an index. To avoid this use "sparse": ``` db.links.ensureIndex({ title: 1},{ sparse:true }); ``` ####Compound Indexes -
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -597,7 +597,17 @@ db.dropDatabase(); To delete specific fields within a document you have to use an update using the $unset operator. ###Indexes ####EXPLAIN Mehtod Get query specifications like objects found, kind of cursor, number of scanned objects, milliseconds it took to do the query. ``` db.links.find({title:'Nettuts+'}).explain(); ``` ####ENSURE INDEX Method To create our own indexes: ``` db.links.ensureIndex({ title: 1}); ``` 1 means to index in ascending order. To see if the index was created: ``` db.system.indexes.find(); ``` -
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -594,4 +594,10 @@ db.collection.drop(); ``` db.dropDatabase(); ``` To delete specific fields within a document you have to use an update using the $unset operator. ###Indexes ####EXPLAIN Mehtod Get query specifications like objects found, kind of cursor, number of scanned objects... ``` db.links.find({title:'Nettuts+'}).explain(); ``` -
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -585,4 +585,13 @@ db.users.findAndModify({ query:{'name.first':/B/}, remove:true }); ``` ####DELETE A COLLECTION ``` db.collection.drop(); ``` ####DELETE A WHOLE DATABASE ``` db.dropDatabase(); ``` To delete specific fields within a document you have to use an update using the $unset operator. -
artieziff revised this gist
Nov 28, 2013 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -573,4 +573,16 @@ db.users.update( {$rename: {$rename: {'random':'new_attribute_name'} }}, false,true ); ``` ###Removing Documents ####REMOVE Operator ``` db.users.remove({'name.first':'John'}); ``` ####FIND AND MODIFY Operator ``` db.users.findAndModify({ query:{'name.first':/B/}, remove:true }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -566,6 +566,7 @@ db.users.update( ); ``` #####RENAME Operator Renames fields ``` db.users.update( {random:true}, -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -564,4 +564,12 @@ db.users.update( {$set: {'logins.$.location':'unknown'} }, false, true ); ``` #####RENAME Operator ``` db.users.update( {random:true}, {$rename: {$rename: {'random':'new_attribute_name'} }}, false,true ); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -558,4 +558,10 @@ db.users.update( {$inc: {'logins.$.minutes':10} }, false, true ); db.users.update( {'logins.minutes':30}, {$set: {'logins.$.location':'unknown'} }, false, true ); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -553,4 +553,9 @@ db.links.update(n,{ $pop:{ tags: -1 } }); /*Pop off the begining*/ ``` #####POSITIONAL Operator ``` db.users.update( {'logins.minutes':20}, {$inc: {'logins.$.minutes':10} }, false, true ); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -550,4 +550,7 @@ var n = {title: 'Nettuts+'}; db.links.update(n,{ $pop:{ tags: 1 } }); /*Pop off the end*/ db.links.update(n,{ $pop:{ tags: -1 } }); /*Pop off the begining*/ ``` #####POSITIONAL Operator ``` ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -499,6 +499,7 @@ db.users.findAndModify({ }); ``` ####Modification Operators ####Array Operators #####PUSH Operator Push new items into an array ``` @@ -540,4 +541,13 @@ Remove multiple values inside an array. var n = {title: 'Nettuts+'}; db.links.update(n,{ $pullAll:{ tags: ['two',three'] } }); ``` #####POP Operator Remove the first or last elements of an array ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $pop:{ tags: 1 } }); /*Pop off the end*/ db.links.update(n,{ $pop:{ tags: -1 } }); /*Pop off the begining*/ ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -526,4 +526,18 @@ To uniquely add multiple values at once to an array var n = {title: 'Nettuts+'}; db.links.update(n,{ $addToSet:{ tags:{ $each: ['one','two'] } } }); ``` #####PULL Operator Remove elements inside an array. ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $pull:{ tags:'four' } }); ``` #####PULL ALL Operator Remove multiple values inside an array. ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $pullAll:{ tags: ['two',three'] } }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -513,3 +513,17 @@ var n = {title: 'Nettuts+'}; db.links.update(n,{ $pushAll:{ tags:['one','two'] } }); ``` #####ADD TO SET Operator If you want your arrays to have unique values. ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $addToSet:{ tags:'code' } }); ``` #####EACH Operator To uniquely add multiple values at once to an array ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $addToSet:{ tags:{ $each: ['one','two'] } } }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -498,3 +498,18 @@ db.users.findAndModify({ fields: {title:1, favourites:1, _id:0} }); ``` ####Modification Operators #####PUSH Operator Push new items into an array ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $push:{ tags:'blog' } }); ``` #####PUSH ALL Operator Push each item in an array to an array in the selected document. ``` var n = {title: 'Nettuts+'}; db.links.update(n,{ $pushAll:{ tags:['one','two'] } }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -488,4 +488,13 @@ db.users.findAndModify({ update:{ $set: {age: 20} }, new: true /*return the updated object, false is the default, and it returns the object before updates*/ }); db.users.findAndModify({ query:{ favourites: 110 }, update:{ $inc: { favourites: 10 } }, sort: { title: 1}, new: true, fields: {title:1, favourites:1, _id:0} }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -486,6 +486,6 @@ This method takes a single object as its parameter. This object has several prop db.users.findAndModify({ query:{ name:'Kate Wills' }, update:{ $set: {age: 20} }, new: true /*return the updated object, false is the default, and it returns the object before updates*/ }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -480,3 +480,12 @@ var bob = db.users.findOne({'name.first':'Bob'}); bob.job = 'Server Admin'; db.users.save(bob); ``` ####Find & Modify Method This method takes a single object as its parameter. This object has several properties. ``` db.users.findAndModify({ query:{ name:'Kate Wills' }, update:{ $set: {age: 20} }, new: true /*return the updated object, false is the default, and it returns the object before updates*/ }); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -473,5 +473,10 @@ This just works with updates by modification. /*First boolean value is used for UPSERTS, the second one for MULTIPARAMETER*/ db.users.update({'name.first':'Jane'}),{$set: {job:'developer'}},false,true); ``` ####Save Method ``` var bob = db.users.findOne({'name.first':'Bob'}); bob.job = 'Server Admin'; db.users.save(bob); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -467,8 +467,11 @@ db.users.update(q,{$set: {job: 'Wev Developer'}}); db.users.update(q,{$unset: {job: 'Wev Developer'}}); ``` #####MULTIPARAMETER Update Multiple Records This just works with updates by modification. ``` /*First boolean value is used for UPSERTS, the second one for MULTIPARAMETER*/ db.users.update({'name.first':'Jane'}),{$set: {job:'developer'}},false,true); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -466,6 +466,9 @@ db.users.update(q,{$set: {job: 'Wev Developer'}}); /*This also works with fields that doesn't exist yet, and to get rid of fields*/ db.users.update(q,{$unset: {job: 'Wev Developer'}}); ``` #####Update Multiple Records This just works with updates by modification. ``` sadf ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -251,7 +251,7 @@ bsondump dump/mydb/users.bson > users.json ``` mongostat ``` ##MONGO COMMANDS (JAVASCRIPT) ``` ###SHOW AVAILABLE DATABASES show dbs -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 20 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -447,6 +447,25 @@ db.users.update({'name.first':'John'},{'job':'developer'}); /*Use this if you want to create a document if it doesn't find the one you passed to update*/ db.users.update({name: 'Kate Wills'},{name: 'Kate Wills', job:'LISP Developer}, true); ``` #####Update By Modification ``` /*INCREMENT*/ var n = {title: 'Nettuts+'}; db.links.find(n,{title:1, favourites:1}); db.links.update(n,{$inc:{favourites: 5}); db.links.update(n,{$inc:{favourites: -5}); /*CHANGE VALUE*/ var q = {name: "Kate Wills}; db.users.update(q,{$set: {job: 'Wev Developer'}}); /*This also works with fields that doesn't exist yet, and to get rid of fields*/ db.users.update(q,{$unset: {job: 'Wev Developer'}}); ``` -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -442,5 +442,11 @@ Replace whatever record it finds that matches the first object. All other fields are removed and replaced with the second object values. ``` db.users.update({'name.first':'John'},{'job':'developer'}); /*UPSERT*/ /*Use this if you want to create a document if it doesn't find the one you passed to update*/ db.users.update({name: 'Kate Wills'},{name: 'Kate Wills',job:'LISP Developer}, true); ``` #####Update By Modification -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -441,6 +441,6 @@ db.links.find().skip(1*3).limit(3); /*GETS ELEMENTS 4,5 & 6*/ Replace whatever record it finds that matches the first object. All other fields are removed and replaced with the second object values. ``` db.users.update({'name.first':'John'},{'job':'developer'}); ``` #####Update By Modification -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -436,11 +436,11 @@ db.links.find().skip(0*3).limit(3); /*GETS ELEMENTS 1,2 & 3*/ db.links.find().skip(1*3).limit(3); /*GETS ELEMENTS 4,5 & 6*/ ``` ###Updating Documents ####Update Method #####Update By Replacement Replace whatever record it finds that matches the first object. All other fields are removed and replaced with the second object values. ``` db.users.update({'name.first':'John'},{job:'developer'}); ``` #####Update By Modification -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -435,4 +435,12 @@ db.links.find({},title:1,favourites:1,_id:0).sort({favourites:1}).limit(1); db.links.find().skip(0*3).limit(3); /*GETS ELEMENTS 1,2 & 3*/ db.links.find().skip(1*3).limit(3); /*GETS ELEMENTS 4,5 & 6*/ ``` ###Updating Documents #####Update Method ######Update By Replacement Replace whatever record it finds that matches the first object. All other fields are removed and replaced with the second object values. ``` db.users.update({'name.first':'John'},{job:'developer'}); ``` ######Update By Modification -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -434,4 +434,5 @@ db.links.find({},title:1,favourites:1,_id:0).sort({favourites:1}).limit(1); ``` db.links.find().skip(0*3).limit(3); /*GETS ELEMENTS 1,2 & 3*/ db.links.find().skip(1*3).limit(3); /*GETS ELEMENTS 4,5 & 6*/ ``` ###Updating Documents -
artieziff revised this gist
Nov 26, 2013 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -430,4 +430,8 @@ db.links.find({},{title:1,favourites: 1, _id:0}).sort({favourites:-1, title:1}); ``` db.links.find({},title:1,favourites:1,_id:0).sort({favourites:1}).limit(1); ``` #####Skip ``` db.links.find().skip(0*3).limit(3); /*GETS ELEMENTS 1,2 & 3*/ db.links.find().skip(1*3).limit(3); /*GETS ELEMENTS 4,5 & 6*/ ```
NewerOlder