InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Linux/Unix Command Line tutorials

  • Awk
  • Curl
  • Edit a file without changing its timestamp on Linux
  • Find which process is listening on a port on Linux
  • Get file size in bytes on Linux
  • How to delete files starting with dash/hyphen
  • How to kill unresponsive ssh session using escape sequence
  • How to show environment variable for a process id (pid)
  • How to sort using a specific field on Linux
  • How to specify environment variable for a command on Linux
  • How to zip/unzip a directory with password
  • ImageMagick
  • Impact on LC_ALL on Linux sort
  • Linux - find listening ports
  • Linux - find top directories by used disk size (excluding size of subdirectories)
  • Linux - how to run a command as different user
  • Linux - list only directories
  • Linux - providing sudo access to a users - some best practices
  • Linux - sending mail from command using mailutils
  • Linux file timestamps
  • Linux how to modify a user using usermod
  • Linux ping a port using netcat
  • Linux replace comma with newline
  • Linux screen - quick start guide
  • Linux what package provides a file
  • Linux/Unix - How to go to previous directory
  • Linux/Unix - find inode number of a file
  • Linux/Unix - truncate a large log file without deleting it
  • Linux/Unix history with date and time
  • Memcache - how to dump all keys and values on command line
  • Mongo - cli quick start guide
  • Perl command line - replace multi line comments
  • Python/Perl/Unix one liners
  • Rsync
  • Ruby gem - handy reference
  • SSH
  • Some handy linux gnu date commands
  • Use watch to monitor a command at some frequency on Linux
  • bower - installation and quick start guide
  • csvkit - parse csv file and data on Linux command line
  • ffmpeg
  • grep without regex (fixed string)
  • redis cli quick start tutorial
  • wget handy commands

Linux find

  • Find recently modified files on Linux
  • Linux - find and delete files older than 30 days
  • Linux - find files containing specific text
  • Linux find - ignore case in name
  • find - exclude directory or file pattern
 
  • Home
  • > Tutorials
  • > Linux/Unix Command Line

Mongo – cli quick start guide

By admin | Last updated on Mar 20, 2016

Using mongo cli utility can be handy way to query mongo db and automate few simple use cases. This article covers some handy commands which can be used with mongo cli utility. This article assumes that you have mongo server installed on local machine. I tested these commands on Mac. But these should work on any other OS.

Install mongo server

Use the following command on Mac:

$ brew install mongo
// To run mongodb one time
$ launchctl load /usr/local/opt/mongodb/homebrew.mxcl.mongodb.plist
// To run on startup
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

Create collection

Create collection in new or existing db:

$ printf "db.createCollection('testcollection1')" | mongo --quiet testdb1
{ "ok" : 1 }
$ printf "db.getCollectionNames()" | mongo --quiet testdb1
[ "system.indexes", "testcollection1" ]
//or
$ printf "show collections"  | mongo --quiet testdb1
system.indexes
testcollection1

drop collection or delete documents in a collection

To drop a collection:

$ printf 'db.testcollection1.drop()' | mongo --quiet testdb1
true

To delete documents in a collection:

$ printf "db.testcollection1.remove({})" | mongo --quiet testdb1
WriteResult({ "nRemoved" : 0 })

List dbs and collections

List dbs and collections in mongo

$ printf "show dbs" | mongo --quiet
local    0.078GB
testdb1  0.078GB
$ printf "show collections"  | mongo --quiet testdb1
system.indexes
testcollection1

insert documents

Insert few test documents in collection

$ printf 'db.testcollection1.insert({key1:"value1"})' | mongo --quiet testdb1
WriteResult({ "nInserted" : 1 })
$ printf 'db.testcollection1.insert({})' | mongo --quiet testdb1
$ printf 'db.testcollection1.insert({})' | mongo --quiet testdb1
// With each document insert an the _id field is automatically created.

Query documents from a collection

Query all documents with limit 1000

$ printf 'db.testcollection1.find({}).limit(1000)' | mongo --quiet testdb1
{ "_id" : ObjectId("55bf6d34e72610341df44d43"), "key1" : "value1" }
{ "_id" : ObjectId("55bf6d8f8d923f4cfbb3294f") }
{ "_id" : ObjectId("55bf6d926a78c27ae1e81d7a") }

You can skip limit(1000) to return all documents.
Query all values where key1 is not equal to “value1”

$ printf 'db.testcollection1.find({key1:{$ne:"value1"}})' | mongo --quiet testdb1

Suggested posts:

  1. How to use exerciser monkey tool for android stress testing
  2. NFS client and server handy commands
  3. Git – how to undo last commit
  4. Android chrome – create website bookmark on home screen
  5. CSS max-width – limit maximum wodth of an element
  6. How to use CasperJS to automate website testing
  7. How to use phantomjs to create site/url snapshot thumbnail
  8. How to add more DNS servers to Wi-Fi interface on Mac
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Linux/Unix Command Line, Mac, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress