Skip to content

Instantly share code, notes, and snippets.

@Humoud
Last active February 19, 2016 19:41
Show Gist options
  • Save Humoud/e381d81a99b564214a8f to your computer and use it in GitHub Desktop.
Save Humoud/e381d81a99b564214a8f to your computer and use it in GitHub Desktop.
A cheat sheet for mongoDB commands and queries.

Mongo DB

Shell Commands

  • List all databases:
db.getMongo().getDBNames();
show databases
show dbs
  • Change databases
use <DB_Name>
  • List all collections
db.getCollectionNames() // returns an array
show collections        // returns a clean output

Ruby Mongo Driver

installed mongoid with: gem install mongo

require 'mongo'

client = Mongo::Client.new([ '192.168.99.100:32776' ], :database => 'development_db')

c = client.database.collection_names
puts "\nAll Collections: #{c}\n"

customers = client[:customers] # Assume have array collection customers
# get all customers in an array format
c_array = customers.with(:read => { :mode => :primary_preferred }).find.to_a
puts "\ncustomers Array: #{c_array}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment