MongoDB Command

In previous section, we have discussed about the Installation of MongoDB. Now in this section we will discussion some MongoDB Command and some operation related to MogoDB.

MongoDB Command

Following are some DB command of Mongo DB

Command      Description
show dbs            Show the database names
show collectionsShows collections names
show users  Number of users in current database
db.cloneDatabase(from host)                                Clones the database
db.copyDatabase(from db, to db, from host)                  Copy the Database
db.currentOp() Current Query/ Operation
db.dropDatabase()         Drop database
db.kilOp()           Kill the current operation  
db.repairDatabase()      Repair the corrupted Database
db.shutdownServer()Shuts down DB server

The basic principle of execution of DB command is while executing a command we have several options like we can basically run a command in background. Background means we run the command and the command gets run like a demon thread. The advantage of Demon thread is suppose I do “db.dropDatabase()”, then any other Query which is getting executed on my database then by that query the lock is not obtained on the database. This is the biggest advantage by option while we are executing a command.

Mongo- Collection Command:

db.collection.drop()Drop the Collection
db.collection.dropIndex(idx_tx)         Drop index idx_tx on Collection “collection”
db.collection.dropIndex()Drop all indexes on the Collection named collection
db.collection.ensureIndex({“aid”:1,”ts”:-1}Create index on aid, ts columns in background ,{background:true})
Db.collection.getIndex()List indexes on collection column
db.collection.renameCollection(new Name)Rename the collection to the new Name
db.collection.drop()Drop the Collection named “collection”
Db.collection.storageSize()Display allocated to the given Collection
db.collection.totalIndexSize()Size in bytes of all index()
db.collection.totalSize()Storage allocated for all data and indexes

Mongo- SQL Mapping:

There are some operation available in database like Creation, Insertion, Select etc. Now we will discuss about these Mapping command.

CREATE TABLE USERS(A Number, b Number)db.createCollection(“users”)
INSERT INTO USERS VALUES(3,5)db.users.insert({a:3,b:5})
SELECT a,b FROM USERSdb.users.find({},{a:1,b:1})
SELECT * FROM USERSdb.users.find()
SELECT * FROM USERS WHERE age=33db.users.find({age:33})
SELECT a, b FROM USERS WHERE age=33db.users.find({age:33},{a:1,b:1})
SELECT * FROM USERS WHERE name LIKE “joe”db.users.find({name:/^joe/})
SELECT * FROM USERS WHERE age>33 AND age<=40db.users.find({“age”:{$gt:33,$lte:40}})
SELECT * FROM USERS ORDER BY NAME DESCdb.users.find().sort({name:-1})
SELECT COUNT (*) FROM USERSdb.users.count()
ALTER TABLE USERS ADD join_date DATETIME                                 db.users.update({},{$set:{ join_date : new Date()}},{multi:true})  
ALTER TABLE USERS DROP COLUMN join_date db.users.update({},{$unset : {join_date : “ ”},multi : true })  
DROP TABLE USERdb.users.drop()
UPDATE  USERS SET status=”fail” where mark < 30db.users.update({age:{$lt: 30}},{$SET: {status: “fail”}},{multi: true})  
UPDATE USERS SET age= age+3 WHERE status=”passdb.users.update({status:”pass”},{$inc:{age:3}},{multi: true})


Parameters=host name (String)- Specifies the host name to copy the current instance .

Use this function to copy a database from a remote to the current database. The command assumes that the remote database has the same name as the current database. For example to clone a  database named import db on a host name host name.


  • The basic format in which format the document is stored in the Mongo DB database .
  • Mongo DB is available for free under the GNU Affero General Public License. The language drivers are available under an Apache License. In addition, 10gen offers commercial licenses for Mongo DB.
  • There are some Interfaces exist in Mongo DB like HTTP Interface and REST Interface which support the manipulation of Mongo DB entries via HTTP GET, HTTP POST, UPDATE, DELETE calls.

Language Supports:

Apart from MongoDB Command, Mongo has some special drivers for a variety of popular programming language and development environments. Web programming language like “Opa” also has a  built-in support for Mongo DB, which is tightly integrated in the language and offers a type safety layer on top Mongo DB. There are also large number of drivers for other programming language and frameworks. Some other languages which support Mongo DB database are:

  • C
  • C++
  • C#
  • Erlang
  • JAVA
  • PERL
  • PHP
  • RUBY

In this section we have discussed about the MongoDB Command. In next section we will discuss some Tools related to MongoDB.

Leave a Reply

Your email address will not be published. Required fields are marked *