-
-
Save johndpope-karhoo/2dba5bf3f348c2b95312671d0835945f to your computer and use it in GitHub Desktop.
Revisions
-
thoragio revised this gist
Apr 25, 2016 . 1 changed file with 2 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 @@ -254,3 +254,5 @@ db.collectionName.aggregate([{$match:{"key":"value"}}, {$limit: 5}, {$project{"_ db.collectionName.aggregate([{$match:{"key":"value"}}, {$sort {"key":1}}, {$limit: 5}, {$project{"_id":0, "key1":1, "key2":1}}]) // with skip stage added db.collectionName.aggregate([{$match:{"key":"value"}}, {$sort {"key":1}}, {$skip: 10}, {$limit: 5}, {$project{"_id":0, "key1":1, "key2":1}}]) // finish notes from "Reshaping Documents in $project Stages" onwards -
thoragio revised this gist
Apr 22, 2016 . 1 changed file with 17 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 @@ -237,3 +237,20 @@ db.getProfilingLevel() db.getProfilingStatus() // and set it from the shell db.setProfilingLevel(1, 40) /* * AGGREGATION */ // simple aggregation query with match stage db.collectionName.aggregate([{$match:{"key":"value"}}]) // with projection stage added db.collectionName.aggregate([{$match:{"key":"value"}}, {$project{"_id":0, "key1":1, "key2":1}}]) // pipeline is an array with one or more documents stipulating different stages // with limit stage added db.collectionName.aggregate([{$match:{"key":"value"}}, {$limit: 5}, {$project{"_id":0, "key1":1, "key2":1}}]) // more efficient to run limit in front of project to avoid projection through a large amount of documents only to return a few // with sort stage added db.collectionName.aggregate([{$match:{"key":"value"}}, {$sort {"key":1}}, {$limit: 5}, {$project{"_id":0, "key1":1, "key2":1}}]) // with skip stage added db.collectionName.aggregate([{$match:{"key":"value"}}, {$sort {"key":1}}, {$skip: 10}, {$limit: 5}, {$project{"_id":0, "key1":1, "key2":1}}]) -
thoragio revised this gist
Apr 18, 2016 . 1 changed file with 17 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 @@ -220,4 +220,20 @@ db.collectionName.find({"location": {$near: {$geometry: {type: "Point", coordina db.collectionName.createIndex("words": "text") db.collectionName.find({$text: {$search: "keyword1 keyword2"}}) // find the best ranked match for the keywords searched db.collectionName.find({$text: {$search: "keyword1 keyword2"}}, {score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}}) /* * PROFILING */ // log slow queries by specifying level of detail (0-none, 1-slow only, 2-all) and millisecond threshold mongod --profile 1 --slowms 20 // then run query, the in mongo shell db.system.profile.find() // find queries longer than 1000ms and sort in reverse order by timestamp db.system.profile.find({millis:{$gt:1000}}).sort({ts:-1}) // get current profiling level from the shell db.getProfilingLevel() db.getProfilingStatus() // and set it from the shell db.setProfilingLevel(1, 40) -
thoragio revised this gist
Apr 18, 2016 . 1 changed file with 2 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 @@ -164,6 +164,8 @@ db.collectionName.createIndex({"key":1}) // ADD COMPOUND INDEX db.collectionName.createIndex({"key1":1, "key2":-1}) // when building compound indexes, following this pattern: // equality fields first, then sort fields, then range fields // LIST INDEXES db.collectionName.getIndexes(); -
thoragio revised this gist
Apr 18, 2016 . 1 changed file with 21 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 @@ -198,4 +198,24 @@ db.collectionName.createIndex({"key":1}, {background: true}) // INDEX SIZE // get the size of the indexes for a collection db.collectionName.stats() db.collectionName.totalIndexSize() // GEOSPATIAL INDEX // set up an index based on location/coordinates in 2 dimensions [x, y] db.collectionName.createIndex({"location":"2d"}) // can also be part of a compound index db.collectionName.createIndex({"location": "2d", "storeType": "grocery"}) // use $near operator in find() queries, limit to first 10 db.collectionName.find({"location":{$near:[x,y]}}).limit(10) // GEOSPATIAL SPHERICAL INDEX // used with longitude and latitude - see geojson.org for details db.collectionName.createIndex({"location": "2dsphere"}) // searching with find involves a few more variables (maxDistance in meters) db.collectionName.find({"location": {$near: {$geometry: {type: "Point", coordinates: [long,lat]}, $maxDistance: 2000}}}) // TEXT INDEX db.collectionName.createIndex("words": "text") db.collectionName.find({$text: {$search: "keyword1 keyword2"}}) // find the best ranked match for the keywords searched db.collectionName.find({$text: {$search: "keyword1 keyword2"}}, {score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}}) -
thoragio revised this gist
Apr 18, 2016 . 1 changed file with 4 additions and 8 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 @@ -195,11 +195,7 @@ db.collectionName.createIndex({"key":1}, {unique:true, sparse: true}) // enables readers/writers to collection while index is being created db.collectionName.createIndex({"key":1}, {background: true}) // INDEX SIZE // get the size of the indexes for a collection db.collectionName.stats() db.collectionName.totalIndexSize() -
thoragio revised this gist
Apr 18, 2016 . 1 changed file with 13 additions and 5 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 @@ -149,6 +149,14 @@ db.collectionName.replaceOne({"key":detail.key}, detail) db.collectionName.explain().find({"key":"value"}) // use the "true" argument also run the query and show metrics on results db.collectionName.explain(true).find({"key":"value"}) // create an explainable object to simplify queries from mongo shell var exp = db.collectionName.explain() exp.find({"key":"value"}) exp.find({"key":"value"}).sort({"key":-1}) // explain() runs in queryPlanner mode by default // can also be run in executionStats and allPlansExecution modes var exp = db.collectionName.explain("executionStats") var exp = db.collectionName.explain("allPlansExecution") // ADD INDEX db.collectionName.createIndex({"key":1}) @@ -183,15 +191,15 @@ db.collectionName.createIndex({"key":1}, {unique:true}) // create a sparse index on a unique but optional key (i.e., not all records will have that key) db.collectionName.createIndex({"key":1}, {unique:true, sparse: true}) // BACKGROUND INDEX CREATION // enables readers/writers to collection while index is being created db.collectionName.createIndex({"key":1}, {background: true}) // get stats on a collection, including index stats (size, etc.) db.stuff.stats(); db.stuff.totalIndexSize(); // shortcut just for index size -
thoragio revised this gist
Apr 18, 2016 . 1 changed file with 4 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 @@ -179,15 +179,16 @@ db.collectionName.find({"keys":{$elemMatch:{"key1":"value", "key2":{$gt:99}}}}) db.collectionName.createIndex({"key":1}, {unique:true}) // can't create if there are already duplicate key values; inserting a duplicate key value afterward gives an error (E11000) // SPARSE INDEXES // create a sparse index on a unique but optional key (i.e., not all records will have that key) db.collectionName.createIndex({"key":1}, {unique:true, sparse: true}) db.students.ensureIndex({'teachers':1}) db.students.find({teachers:{$all:[0,1]}}).explain() // create an index in the background (so queries can still be run against the collection) db.stuff.createIndex({thing:1}, {background:true}); -
thoragio revised this gist
Apr 15, 2016 . 1 changed file with 12 additions and 11 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 @@ -31,6 +31,8 @@ db.collectionName.insertMany([{}, {}, {}], {ordered:false}) db.collectionName.remove({}) // remove a specific (set of) record(s) db.collectionName.remove({"key":"value"}) // remove just one record db.collectionName.remove({"key":"value"}, {justOne:true}) // remove all records and delete collection from collections list db.collectionName.drop() // delete an entire DB @@ -165,25 +167,24 @@ db.collectionName.dropIndex({"key":1}) // created when one of the keys of an index is an array // can't have a compound index where both keys in a single document are arrays (only one array per document per compound index) // MULTIKEY INDEXES & DOT NOTATION // create index with a key and sub-key (dot notation) db.collectionName.createIndex({"keys.key":-1}); // find command with a key and sub-key db.collectionName.find({"keys.key":{$gt:99}}) // find docs with an array element set to a value (keys:key1 has key2 > 99) db.collectionName.find({"keys":{$elemMatch:{"key1":"value", "key2":{$gt:99}}}}) // UNIQUE INDEXES db.collectionName.createIndex({"key":1}, {unique:true}) // can't create if there are already duplicate key values; inserting a duplicate key value afterward gives an error (E11000) db.students.ensureIndex({'teachers':1}) db.students.find({teachers:{$all:[0,1]}}).explain() // create a sparse index on a unique but optional key db.stuff.createIndex({optionalThing:1}, {unique:true, sparse:true}); -
thoragio revised this gist
Apr 15, 2016 . 1 changed file with 26 additions and 9 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 @@ -138,24 +138,41 @@ db.collectionName.updateOne({"key":"value"}, {$set: {"key2":"value2"}}, {upsert: db.collectionName.replaceOne({"key":detail.key}, detail) // where detail is a variable containing all document information (key/value pairs) for the operation /* * INDEXES */ // EXPLAIN // shows how the database does what is requested of it db.collectionName.explain().find({"key":"value"}) // use the "true" argument also run the query and show metrics on results db.collectionName.explain(true).find({"key":"value"}) // ADD INDEX db.collectionName.createIndex({"key":1}) // 1 means ascending order // ADD COMPOUND INDEX db.collectionName.createIndex({"key1":1, "key2":-1}) // LIST INDEXES db.collectionName.getIndexes(); // DELETE INDEX db.collectionName.dropIndex({"key":1}) // MULTIKEY INDEXES // created when one of the keys of an index is an array // can't have a compound index where both keys in a single document are arrays (only one array per document per compound index) db.students.ensureIndex({'teachers':1}) db.students.find({teachers:{$all:[0,1]}}).explain() db.students.dropIndex({student_id:1, class_id:-1}) // create index with a key and sub-key (dot notation) -
thoragio revised this gist
Apr 15, 2016 . 1 changed file with 2 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 @@ -3,6 +3,8 @@ ulimit -n 2048 && mongod // ctrl-c ends in same window, but can also use killall mongod // start mongod with data directory in another location mongod -dbpath /path/to/data/dir // open command line interface (mongo shell) mongo // restore a DB dump from a binary (BSON) dump file -
thoragio revised this gist
Apr 15, 2016 . 1 changed file with 2 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 @@ -1,6 +1,8 @@ // MONGO COMMAND LINE // start mongod ulimit -n 2048 && mongod // ctrl-c ends in same window, but can also use killall mongod // open command line interface (mongo shell) mongo // restore a DB dump from a binary (BSON) dump file -
thoragio revised this gist
Apr 1, 2016 . 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 @@ -11,6 +11,8 @@ mongoimport -d dbName -c collectionName dumpFileName.json // CREATING DOCUMENTS // see what DBs are available show dbs // show currently selected DB db // select a DB to use, even if it does not exist use dbName // see what collections are available within selected DB @@ -27,6 +29,9 @@ db.collectionName.remove({}) db.collectionName.remove({"key":"value"}) // remove all records and delete collection from collections list db.collectionName.drop() // delete an entire DB use dbName db.dropDatabase() // READING DOCUMENTS // find the first record in the collection -
thoragio revised this gist
Mar 30, 2016 . 1 changed file with 4 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 @@ -3,6 +3,10 @@ ulimit -n 2048 && mongod // open command line interface (mongo shell) mongo // restore a DB dump from a binary (BSON) dump file mongorestore dumpFileName // restore a DB dump from a JSON dump file mongoimport -d dbName -c collectionName dumpFileName.json // CREATING DOCUMENTS // see what DBs are available -
thoragio revised this gist
Mar 29, 2016 . 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 @@ -116,7 +116,7 @@ db.collectionName.updateOne({"key":"value"}, {$push: {"key2": {$each: [{"key2a": db.collectionName.updateOne({"key":"value"}, {$push: {"key2": {$each: [{"key2a": "caseC", "key2b": 34}], $position: 0, $slice: 5}}}) // REMOVE NULL FIELDS FROM DOCUMENTS IN A COLLECTION db.collectionName.updateMany({"key":null}, {$unset{"key":""}}) // UPSERT db.collectionName.updateOne({"key":"value"}, {$set: {"key2":"value2"}}, {upsert: true}) -
thoragio revised this gist
Mar 29, 2016 . 1 changed file with 28 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 @@ -98,6 +98,34 @@ db.collectionName.find({"key1": {$elemMatch: {"key1a": "value", "key1a2": {$gt: // $elemMatch ensures all criteria are matched within a single element of an array field // UPDATING DOCUMENTS // updateOne(), updateMany(), replaceOne() // first specify a filter/selector document (key), then specify how with an update operator ($set) db.collectionName.updateOne({"key":"value"}, {$set: {"key2": "value2"}}) // FIELD UPDATE OPERATORS // $set, $unset, $inc, $mul, $rename, $setOnInsert, $min, $max, $currentDate db.collectionName.updateOne({"key":"value"}, {$inc: {"key2": 3, "key3": 101}}) // more info: https://docs.mongodb.org/v3.2/reference/operator/update/ // ARRAY FIELD UPDATE db.collectionName.updateOne({"key":"value"}, {$push: {"key2": {"key2a": "caseA", "key2b": 55}}}) // use $each when pushing multiple db.collectionName.updateOne({"key":"value"}, {$push: {"key2": {$each: [{"key2a": "caseA", "key2b": 55}, {"key2a": "caseB", "key2b": 76}]}}}) // $slice can be used with $push and $each to trim down the array to the first set of records // $position is needed to ensure inserted value goes to the front of the array rather than the end db.collectionName.updateOne({"key":"value"}, {$push: {"key2": {$each: [{"key2a": "caseC", "key2b": 34}], $position: 0, $slice: 5}}}) // REMOVE NULL FIELDS FROM DOCUMENTS IN A COLLECTION db.collectionName.updateMany({"key":null}, {$unset{"key:""}}) // UPSERT db.collectionName.updateOne({"key":"value"}, {$set: {"key2":"value2"}}, {upsert: true}) // REPLACE ONE db.collectionName.replaceOne({"key":detail.key}, detail) // where detail is a variable containing all document information (key/value pairs) for the operation -
thoragio revised this gist
Mar 29, 2016 . 1 changed file with 32 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 @@ -67,6 +67,38 @@ db.collectionName.find({"key":{$type:"string"}}) // numeric values also exist for the $type BSON equivalents, see: // https://docs.mongodb.org/v3.0/reference/operator/query/type/ // LOGICAL OPERATORS // $or, $and, $not, $nor db.collectionName.find({$or:[{"key1":{$gt:90}}, {"key2":{$gt:80}}]}) // $and is sometimes superfluous. this query: db.collectionName.find({$and:[{"key1":{$gt:90}}, {"key2":{$gt:80}}]}) // is the same as this one: db.collectionName.find({"key1":{$gt:90}, "key2":{$gt:80}}) // $and is used to specify multiple constraints on the same key db.collectionName.find({$and:[{"key1":{$ne:null}}, {"key1":{$exists:true}}]}) // REGEX OPERATOR // $regex db.collectionName.find({"key1":{$regex:/^Text\s.*/}}) // ARRAY OPERATORS // $all, $elemMatch, $size db.collectionName.find({"key1": {$all: ["caseA", "caseB", "caseC"]}}) // $size enables matching based on the length of an array db.collectionName.find({"key1": {$size: 2}}) /* * $elemMatch is used for documents with a field with an array that hold embedded documents within it * key1: [ {"key1a": "caseA1", "key1b": "caseA2"}, * {"key1a": "caseB1", "key1b": "caseB2"} ] */ db.collectionName.find({"key1": {"key1a": "value", "key1a2": {$gt: 10}}}) // above will not find a specific embedded document that matches both criteria, // it will just find an array where the criteria are matched in any combination of the fields in the embedded documents db.collectionName.find({"key1": {$elemMatch: {"key1a": "value", "key1a2": {$gt: 10}}}}) // $elemMatch ensures all criteria are matched within a single element of an array field // UPDATING DOCUMENTS -
thoragio revised this gist
Mar 25, 2016 . 1 changed file with 6 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 @@ -60,7 +60,12 @@ db.collectionName.find({"key":{$ne:"something"}}) // the $in comparison operator returns all documents with fields == to one of the values specified in the array of values db.collectionName.find({"key":{$in:["caseA", "caseB", "caseC"]}}) // ELEMENT OPERATORS // $exists, $type db.collectionName.find({"key":{$exists:true}}) db.collectionName.find({"key":{$type:"string"}}) // numeric values also exist for the $type BSON equivalents, see: // https://docs.mongodb.org/v3.0/reference/operator/query/type/ -
thoragio revised this gist
Mar 25, 2016 . 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 @@ -49,8 +49,8 @@ db.collectionName.find({"key":"value"},{"key2":1}) // projection to limit data db.collectionName.find({"key":"value"},{"key2":1, "_id":0}) // and leave out the _id //COMPARISON OPERATORS // $eq, $gt, $lt, $gte, $lte, $ne, $in, $nin db.collectionName.find({"key":{$gt:100}}) // find key value > 100 // ranges can also be specified db.collectionName.find({"key":{$gt:100, $lt:150}}) // find key value > 100 and < 150 // or different field search values -
thoragio revised this gist
Mar 25, 2016 . 1 changed file with 13 additions and 6 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 @@ -48,15 +48,22 @@ var doc = function() {return c.hasNext() ? c.next() : null;} // nice doc() func db.collectionName.find({"key":"value"},{"key2":1}) // projection to limit data sent back for query results (in this case, only key2 is returned) db.collectionName.find({"key":"value"},{"key2":1, "_id":0}) // and leave out the _id //COMPARISON OPERATORS db.collectionName.find({"key":{$gt:100}}) // find key value > 100 // other operators: $eq, $lt, $gte, $lte, $ne, $in, $nin // ranges can also be specified db.collectionName.find({"key":{$gt:100, $lt:150}}) // find key value > 100 and < 150 // or different field search values db.collectionName.find({"key1":{$gte:30}, "key2":{$gte:80}}) // the $ne comparison operator returns all documents with fields != to the value specified && without that field at all db.collectionName.find({"key":{$ne:"something"}}) // the $in comparison operator returns all documents with fields == to one of the values specified in the array of values db.collectionName.find({"key":{$in:["caseA", "caseB", "caseC"]}}) db.movieDetails.updateMany({"imdb.votes":{$lt:10000}, $and: [ {"year":{$gte:2010}}, {"year":{$lte:2013}}], "tomato.consensus":null}, {$unset: {"tomato.consensus":""}}) -
thoragio revised this gist
Mar 24, 2016 . 1 changed file with 53 additions and 17 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 @@ -1,20 +1,56 @@ // MONGO COMMAND LINE // start mongod ulimit -n 2048 && mongod // open command line interface (mongo shell) mongo // CREATING DOCUMENTS // see what DBs are available show dbs // select a DB to use, even if it does not exist use dbName // see what collections are available within selected DB show collections // as with the DB, a new collection is created just by referring to it db.collectionName.insertOne({"key1":"value1", "key2":"value2"}) db.collectionName.insertMany([{}, {}, {}]) db.collectionName.insertMany([{}, {}, {}], {ordered:false}) // DELETING DOCUMENTS // remove all records db.collectionName.remove({}) // remove a specific (set of) record(s) db.collectionName.remove({"key":"value"}) // remove all records and delete collection from collections list db.collectionName.drop() // READING DOCUMENTS // find the first record in the collection db.collectionName.findOne() // first argument to find is called the "query document", which can have one or more fields (selectors) to restrict the result set db.collectionName.find({"key":"value1", "key.nested":"value2"}) db.collectionName.find({}).count() db.collectionName.find({}).pretty() // document queries can also search for an array of results, in this case an exact match of two values // order matters, will only find documents with value1 & value2 && in that order, this happens because search values are enclosed in an array db.collectionName.find({"key":["value1", "value2"]}) // when searching array, find a value at a specific spot (element) in the array (in this case, the first element in the array) db.collectionName.find({"key.0":"value1"}) // CURSORS var c = db.collectionName.find({"key":"value"}) c.hasNext() // boolean stating whether or not there are more documents in the cursor c.next() // displays the next document in the cursor c.objsLeftInBatch() // show how many objects are left in batch var doc = function() {return c.hasNext() ? c.next() : null;} // nice doc() function to display next document in batch // PROJECTION db.collectionName.find({"key":"value"},{"key2":1}) // projection to limit data sent back for query results (in this case, only key2 is returned) db.collectionName.find({"key":"value"},{"key2":1, "_id":0}) // and leave out the _id db.movieDetails.find({"tomato.meter":{$gte:99}, "runtime":{$gte:120}}) // using comparison operators db.movieDetails.find({"rated":{$ne:"UNRATED"}}) // $ne is not equal to and also includes all documents WITHOUT the searched field db.movieDetails.find({"rated":{$in:["G", "PG"]}}).count() // $in to find matches from an array of options -
thoragio revised this gist
Mar 23, 2016 . 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 @@ -10,7 +10,7 @@ c.hasNext() c.next() db.COLLECTION-NAME.insertOne() db.COLLECTION-NAME.insertMany() db.COLLECTION-NAME.insertMany([{}, {}, {}], {ordered:false}) db.movieDetails.find({writers: ["Joel Cohen", "Ethan Cohen"]}) // order matters, will only find documents with Joel & Ethan (no other writers) && in that order, this happens because search values are enclosed in an array db.movieDetails.find({"actor.0":"Jeff Bridges"}) // finds records with JB as the first actor in the list db.movieDetails.find({"actor":"Jack Black"},{"title":1}) // projection to limit data sent back for query results (in this case, only the title is returned) -
thoragio revised this gist
Feb 13, 2016 . 1 changed file with 4 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 @@ -51,3 +51,7 @@ db.stuff.createIndex({optionalThing:1}, {unique:true, sparse:true}); // create an index in the background (so queries can still be run against the collection) db.stuff.createIndex({thing:1}, {background:true}); // get stats on a collection, including index stats (size, etc.) db.stuff.stats(); db.stuff.totalIndexSize(); // shortcut just for index size -
thoragio revised this gist
Feb 12, 2016 . 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 @@ -48,3 +48,6 @@ db.stuff.createIndex({thing:1}, {unique:true}); // create a sparse index on a unique but optional key db.stuff.createIndex({optionalThing:1}, {unique:true, sparse:true}); // create an index in the background (so queries can still be run against the collection) db.stuff.createIndex({thing:1}, {background:true}); -
thoragio revised this gist
Feb 11, 2016 . 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 @@ -38,4 +38,13 @@ db.students.getIndexes() db.students.dropIndex({student_id:1, class_id:-1}) // create index with a key and sub-key (dot notation) db.people.createIndex({"work_history.company":-1}); // remove one occurrence of an item db.stuff.remove({thing: 'apple'}, {justOne: true}); // create a unique index db.stuff.createIndex({thing:1}, {unique:true}); // create a sparse index on a unique but optional key db.stuff.createIndex({optionalThing:1}, {unique:true, sparse:true}); -
thoragio revised this gist
Feb 11, 2016 . 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 @@ -36,3 +36,6 @@ db.students.createIndex({student_id:1, class_id:-1}) db.students.getIndexes() db.students.dropIndex({student_id:1, class_id:-1}) // create index with a key and sub-key (dot notation) db.people.createIndex({"work_history.company":-1}) -
thoragio revised this gist
Feb 11, 2016 . No changes.There are no files selected for viewing
-
thoragio revised this gist
Feb 11, 2016 . 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 @@ -25,7 +25,7 @@ db.movieDetails.updateOne({title: "The Martian"}, { $set: { "awards" : {"wins" : db.movieDetails.updateMany({"imdb.votes":{$lt:10000}, $and: [ {"year":{$gte:2010}}, {"year":{$lte:2013}}], "tomato.consensus":null}, {$unset: {"tomato.consensus":""}}) db.students.ensureIndex({'teachers':1}) db.students.find({teachers:{$all:[0,1]}}).explain() db.students.createIndex({student_id:1}) // user true flag to get more details on the query, like number of documents examined -
thoragio created this gist
Feb 11, 2016 .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 @@ -0,0 +1,38 @@ db.COLLECTION-NAME.findOne() db.COLLECTION-NAME.find({"key":"value"}) db.COLLECTION-NAME.find().count() db.COLLECTION-NAME.find().pretty() db.COLLECTION-NAME.remove({}) db.COLLECTION-NAME.drop() use DB-NAME (select a DB to use, even if it does not exist) var c = db.COLLECTION-NAME.find({"key":"value"}) c.hasNext() c.next() db.COLLECTION-NAME.insertOne() db.COLLECTION-NAME.insertMany() db.COLLECTION-NAME.insertMany([{}], {ordered:false}) db.movieDetails.find({writers: ["Joel Cohen", "Ethan Cohen"]}) // order matters, will only find documents with Joel & Ethan (no other writers) && in that order, this happens because search values are enclosed in an array db.movieDetails.find({"actor.0":"Jeff Bridges"}) // finds records with JB as the first actor in the list db.movieDetails.find({"actor":"Jack Black"},{"title":1}) // projection to limit data sent back for query results (in this case, only the title is returned) db.movieDetails.find({"actor":"Jack Black"},{"title":1, "_id":0}) // and leave out the _id db.movieDetails.find({"tomato.meter":{$gte:99}, "runtime":{$gte:120}}) // using comparison operators db.movieDetails.find({"rated":{$ne:"UNRATED"}}) // $ne is not equal to and also includes all documents WITHOUT the searched field db.movieDetails.find({"rated":{$in:["G", "PG"]}}).count() // $in to find matches from an array of options db.movieDetails.find( { $and: [ { "genres": { $all: ["Comedy", "Crime"] } }, {"genres": { $size: 2} } ] } ) db.movieDetails.find( { $and: [ { "genres.0": "Comedy" }, { "genres.1": "Crime" }, { "genres": { $size: 2 } } ] } ) db.movieDetails.updateOne({title: "The Martian"}, { $set: { "awards" : {"wins" : 10 } } } ) db.movieDetails.updateMany({"imdb.votes":{$lt:10000}, $and: [ {"year":{$gte:2010}}, {"year":{$lte:2013}}], "tomato.consensus":null}, {$unset: {"tomato.consensus":""}}) db.students.ensureIndex({'teachers':1}) db.students.find({'teachers:{$all:[0,1]}}).explain() db.students.createIndex({student_id:1}) // user true flag to get more details on the query, like number of documents examined db.students.explain(true).find({student_id:5}) //compound db.students.createIndex({student_id:1, class_id:-1}) db.students.getIndexes() db.students.dropIndex({student_id:1, class_id:-1})