The .mongorc.js File

The .mongorc.js File

While working in MongoDB shell, if you are frequently wondering about which database you are working on, you can obviously find out typing ‘db’ at the command prompt. But if you are lazy like me, you can change your command prompt to reflect the database name by modifying the prompt function as follows:

[box]

prompt=function () {
 return db+'> ';
 }

[/box]

You can enhance this further to have the prompt display the current timestamp along with the database name to time your queries as follows:

[box]

prompt=function () {
 return db+':'+(new Date())+'> ';
 }

[/box]

If you get used to this and do not want to reprogram the prompt function every time you start your MongoDB shell, you can add a .mongorc.js file in your home directory and add the modified prompt function.

Now, every time you start your MongoDB shell, your prompt will show the database name and the current system time.

MongoDB Supported Data Types

November 23, 2013
MongoDB Supported Data Types

MongoDB adds support for a number of additional data types while keeping JSON’s essential key/value pair nature. Exactly how values of each type are represented varies by language, but this is a list of the commonly supported types and how they are represented as part of a document in the shell. The most common types are:

null

Null can be used to represent both a null value and a nonexistent field:

{“x” : null}

boolean

There is a boolean type, which can be used for the values true and false:

{“x” : true}

number

The shell defaults to using 64-bit floating point numbers. Thus, these numbers look “normal” in the shell:

{“x” : 3.14}

or:

{“x” : 3}

integers

For integers, use the NumberInt or NumberLong classes, which represent 4-byte or 8-byte signed integers, respectively.

{“x” : NumberInt(“3”)}

{“x” : NumberLong(“3”)}

string

Any string of UTF-8 characters can be represented using the string type:

{“x” : “foobar”}

date

Dates are stored as milliseconds since the epoch. The time zone is not stored:

{“x” : new Date()}

regular expression

Queries can use regular expressions using JavaScript’s regular expression syntax:”

“{“x” : /foobar/i}

array

Sets or lists of values can be represented as arrays:

{“x” : [“a”, “b”, “c”]}

embedded document

Documents can contain entire documents embedded as values in a parent document:

{“x” : {“foo” : “bar”}}

object id

An object id is a 12-byte ID for documents. See the section _id and ObjectIds for details:

{“x” : ObjectId()}

There are also a few less common types that you may need, including:

binary data

Binary data is a string of arbitrary bytes. It cannot be manipulated from the shell. Binary data is the only way to save non-UTF-8 strings to the database.

code

Queries and documents can also contain arbitrary JavaScript code:

{“x” : function() { /* … */ }}

Excerpt From

Kristina Chodorow. “MongoDB_ The Definitive Guide, 2nd Edition.epub.” iBooks.

Document Databases

Explores the difference between the leading document databases, CouchDB and MongoDB

mongodb.pngExcerpt from Mongo DB in Action – “Few databases identify themselves as document databases. The other well-known document database apart from MongoDB is Apache’s CouchDB.

CouchDB’s document model is similar, although data is stored in plain text as JSON, whereas MongoDB uses the BSON binary format.

Like MongoDB, CouchDB supports secondary indexes; the difference is that the indexes in CouchDB are defined by writing map-reduce functions, which is more involved than the declarative syntax used by MySQL and MongoDB.

They also scale differently. CouchDB doesn’t partition data across machines; rather, each CouchDB node is a complete replica of every other.”