The little MongoDB book summary

Setup steps (for windows):
1. Download mongodb from official page.
2. Extract somewhere
3. mongod.exe – server, mongo.exe – client shell
4. Create “mongodb.config” near with mongo.exe
5. Append “mongodb.config” with line “dbpath=<any local existed path>”, here data will be stored.
6. Launch mongod.exe with “–config <path to mongodb.config>” key
7. Launch mongo.exe, and enter command db.version() to make sure all is ok.

Mongodb internal entities:

server -> databases -> collections (tables) -> document (row) -> fields (collumn)

Command “db.stats()” – self descriptive
Command “” – shows all
Command “use <db name>” – switch database
Command “db.<collection name>.insert(<JSON lines to insert>)” – inserts new document into collection

_id” field is automatically added (add manually is also possible)

Command “db.<collection name>.find()” – returns list of documents

Query selector – like SQL’s where – JSON object, in format “{<field name>: <value name>}

Command “db.<collection name>.remove()” – clears collection

Operators $lt, $lte, $gt, $gte, $ne – within selector are used to specify <, <=, >, >=, !=, in format “{<field name>: {$lt: <value name>}}”

Operator $exists checks whether some field is presented within document.
Operator $or like SQL’s “or” operator. Like (all items with field1==value1 OR field2==value2):

db.<collection name>.find( $or: [ { <field1>: <value1> }, { <field2>: <value2> } ] )
Command “db.unicorns.update( { <field1>: <var1> }, { <field2>: <var2> } )” finds first document by { <field1>: <var1> } selector and replaces it with { <field2>: <value2> } document.

Operator $set used to update only one specified field of selected document, like:
db.<collection name>.update({<field1>: <val1>}, {$set: {<field2>: <value2>}})

Operator $inc increments a field with some specified value, like:

db.<collection name>.update({<selector>}, {$inc: {<field>: <value>}})

Operator $push appends some value into specified field, in case field’s types is array, like:
db.<collection name>.update({<selector>}, {$push: {<field>: <value>}})

Upserts – in case some document is found it is updated, if not – it is added, with a third true parameter, like:
Command “db.unicorns.update( { <field1>: <var1> }, { <field2>: <var2> }, true )

Multiple updates – update’s fourth parameter should be true, like
Command “db.unicorns.update( { <field1>: <var1> }, { <field2>: <var2> }, true, true )

Find detailed

Field selection
Command “db.<collection name>.find(<selector>, <selector which fields should be shown>);

Command “db.<collection name>.find().sort({<field>: -1})” – sorts documents, 1- ascending, -1 – descending order.

Command “db.unicorns.find().limit(<number of documents>).skip(<number of documents to skip>)” – allows to skip some count of documents and set a count of returned documents

Command “db.unicorns.find(<selector>).count()” – return a number of documents selected.

It is possible to insert document into some separate field.
DBRef – pointer to another document

Crapped collections (old data are dropped)


Command “db.<collection name>.ensureIndex({<field name>: 1});” creates index
Command “db.<collection name>.dropIndex({<field name>: 1});” drops index
Command “db.<collection name>.find().explain()” – cursor’s command describes returned set.


About DmitryKrinitsyn
Software developer

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: