Useful MongoDB commands

How to install MongoDB on Mac OS X I showed already here:
https://robert-reiz.com/2011/08/11/installing-mongodb-on-mac-ox-x-lion/

If everything is installed correctly, you can start the server process as root with this command:

mongod

and the client as no root with this command:

mongo

By default you are logged in into the “test” database. You can show all dbs with this command:

show dbs

you can switch to an existing DB or create a new DB with this command:

use mynewdb

With this command here you can show alle “collections” inside of the DB:

show collections

You can make an insert with this command:

db.users.save( { name : "myname" } )

If the collections “users” does not exist it will be created with the first call. With this command you can see all entries in the collection “users”:

db.users.find()

With count() you can see how many elements are in the collection.

db.users.count()

And with “remove” you can remove alle elements from a collections.

db.users.remove()

And with drop you can drop the entire collection.

db.users.drop()

Published by Robert Reiz

CEO @ VersionEye. Passionated software developer since 1998.

Leave a comment