Skip to content

Instantly share code, notes, and snippets.

@bliotti
Forked from artieziff/mongodb.md
Created November 18, 2019 05:35
Show Gist options
  • Select an option

  • Save bliotti/157a1441c6443b7abcbc55ced0038342 to your computer and use it in GitHub Desktop.

Select an option

Save bliotti/157a1441c6443b7abcbc55ced0038342 to your computer and use it in GitHub Desktop.

Revisions

  1. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb.md
    Original 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.
    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.
  2. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion mongodb.md
    Original 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();
    ```
  3. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 18 additions and 2 deletions.
    20 changes: 18 additions & 2 deletions mongodb.md
    Original 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 our own indexes:
    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

  4. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion mongodb.md
    Original 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...
    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();
    ```
  5. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion mongodb.md
    Original 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.
    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();
    ```
  6. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion mongodb.md
    Original 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.
  7. @artieziff artieziff revised this gist Nov 28, 2013. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions mongodb.md
    Original 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
    });
    ```
  8. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mongodb.md
    Original 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},
  9. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions mongodb.md
    Original 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
    );
    ```
  10. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions mongodb.md
    Original 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
    );
    ```
  11. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions mongodb.md
    Original 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
    );
    ```
  12. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions mongodb.md
    Original 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
    ```
    ```
  13. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions mongodb.md
    Original 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*/
    ```
  14. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions mongodb.md
    Original 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'] } });
    ```
  15. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions mongodb.md
    Original 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'] } } });
    ```
  16. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions mongodb.md
    Original 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'] } });
    ```
  17. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions mongodb.md
    Original 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}
    });
    ```
  18. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb.md
    Original 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*/
    new: true /*return the updated object, false is the default, and it returns the object before updates*/
    });
    ```
  19. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions mongodb.md
    Original 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*/
    });
    ```
  20. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions mongodb.md
    Original 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);
    ```
  21. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions mongodb.md
    Original 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'}});
    ```
    #####Update Multiple Records
    #####MULTIPARAMETER Update Multiple Records
    This just works with updates by modification.
    ```
    sadf
    /*First boolean value is used for UPSERTS, the second one for MULTIPARAMETER*/
    db.users.update({'name.first':'Jane'}),{$set: {job:'developer'}},false,true);
    ```
  22. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions mongodb.md
    Original 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
    ```
  23. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb.md
    Original file line number Diff line number Diff line change
    @@ -251,7 +251,7 @@ bsondump dump/mydb/users.bson > users.json
    ```
    mongostat
    ```
    ##MONGO SHELL COMMANDS (JAVASCRIPT)
    ##MONGO COMMANDS (JAVASCRIPT)
    ```
    ###SHOW AVAILABLE DATABASES
    show dbs
  24. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion mongodb.md
    Original 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);
    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'}});
    ```
  25. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions mongodb.md
    Original 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
  26. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb.md
    Original 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'});
    db.users.update({'name.first':'John'},{'job':'developer'});
    ```
    #####Update By Modification
  27. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions mongodb.md
    Original 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
    ####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
    #####Update By Modification
  28. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion mongodb.md
    Original 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
    ###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
  29. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion mongodb.md
    Original 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
  30. @artieziff artieziff revised this gist Nov 26, 2013. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion mongodb.md
    Original 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*/
    ```