Skip to content

Instantly share code, notes, and snippets.

@johndpope-karhoo
Forked from thoragio/mongo-notes.js
Created October 26, 2016 17:31
Show Gist options
  • Select an option

  • Save johndpope-karhoo/2dba5bf3f348c2b95312671d0835945f to your computer and use it in GitHub Desktop.

Select an option

Save johndpope-karhoo/2dba5bf3f348c2b95312671d0835945f to your computer and use it in GitHub Desktop.
Mongo Notes
// 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
//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":""}})
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})
// 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});
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment