mongo - The Interactive Shell

Introduction

The MongoDB distribution includes bin/mongo, the MongoDB interactive shell. This utility is a JavaScript shell that allows you to issue commands to MongoDB from the command line. (Basically, it is an extended SpiderMonkey shell.)

The shell is useful for:

  • inspecting a database's contents
  • testing queries
  • creating indices
  • other administrative functions.

When you see sample code in this wiki and it looks like JavaScript, assume it is a shell example. See the driver syntax table for a chart that can be used to convert those examples to any language.

More Information

Some Notes on Datatypes in the Shell

Numbers

The shell treats all numbers as floating-point values. If you have long/integer BSON data from the database you may see something like this:

"bytes" : {
   "floatApprox" : 575175
}

or something like this for larger numbers:

"bytes" : {
   "floatApprox" : 5284376243087482000,
   "top" : 1230364721,
   "bottom" : 4240317554
}

In addition, setting/incrementing any number will (most likely) change the data type to a floating point value

Dates

The Date() function returns a string and a "new Date()" will return an object (which is what you should use to store values).

> Date()
Sun May 02 2010 19:07:40 GMT-0700 (Pacific Daylight Time)
> new Date()
"Sun May 02 2010 19:07:43 GMT-0700 (Pacific Daylight Time)"
> typeof(new Date())
object
> typeof(Date())
string

BinData

The BSON BinData datatype is represented via class BinData in the shell. Run help misc for more information.

Presentations


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus