Basic

robomongo.org – shows database

Database Names cannot contain any characters /\. “$*<>:|? and null character, cannot be empty and must have less than 64 characters

Field Names cannot contain dots and null characters

Replica Sets can have up to 50 members; up to 7 voting members

Validation: duplicated _id, syntax errors, documents over 16mb

$ mongo >

> help – show list of commands

> use databaseName – use database or creates it if doesn’t exist

> mongorestore – creates a new database or add data to an existing database

> db – returns the current database

> show dbs – show list of databases

> db.collectionName.createIndex({ document }) – create a collection if it doesn’t exist

> db.collectionName.insert({ document }) – save document into a collection

> db.collectionName.find({ }) – finds all document

> db.collectionName.findOne({ }) – find the only one document

> db.collectionName.find().pretty() – display an easy-to-read format

> db.collectionName.find({ “field“: “value” }) / .find({ “arrayFieldName“: “arrayValue” }) / .find({ “objectFieldName.objectName“: “objectValue” }) – finding a document

> db.collectionName.remove({ “field“: “value” }) – delete a single document

> db.collectionName.update({“queryField“: “value“}, {“$set“: {“field“: “newValue“}} ) – update a document

> db.collectionName.update({ “queryField“: “value” }, { “field“: “newValue” } ) – import data or overwriting a document

> db.collectionName.update({“queryField“: “value“}, {“$set”: {“field“: “newValue“}}, {“multi“: true} ) – update multiple document

> db.collectionName.update({ “queryfield“: “value” }, { “$inc“: {“field“: 1} }) – increment or update a document’s count

> db.collectionName.update({ “queryfield“: “value” }, { “$inc”: {“field“: 1} }, { “upsert“: true }) – creates a new doc or updates it

> db.collectionName.update({ }, { “$unset“: {“field“: “”} }, { “multi”: true }) – removes specific fields

> db.collectionName.update({ }, { “$rename“: {“oldFieldName“: “newFieldName“} }, { “multi”: true }) – update or change field names

> db.collectionName.update({“queryField“: “value“}, {“$set“: {“arrayFieldName.index#“: “newArrayValue “}} ) – update single array value

> db.collectionName.update({“queryField“: “value“}, {“$set“: {“arrayFieldName.$“: “newArrayValue“}}, { “multi”: true }) – update array value on multiple arrays

> db.collectionName.update({“queryField“: “value“}, {“$set“: {“objectFieldName.objectName“: “newObjectValue“}} ) – updating an embedded / object value

  • Update Operators: $max $min   $mul

> db.collectionName.update({“queryField“: “value“}, {“$mul“: {“field“: value}} ) – multiply the value field

> db.collectionName.update({“queryField“: “value“}, {“$pop“: {“arrayFieldName“: #}} ) – removing the first(-1)/last(1) value of an array

> db.collectionName.update({“queryField“: “value“}, {“$push“: {“arrayFieldName“: “newArrayValue “}} ) – adding values to the end of an array

> db.collectionName.update({“queryField“: “value“}, {“$addToSet“: {“arrayFieldName“: “newArrayValue “}} ) – adding unique values to an array

> db.collectionName.update({“queryField“: “value“}, {“$pull“: {“arrayFieldName“: “arrayValue“}} ) – removing values from an array

> db.collectionName.find({ “queryField1“: “value“, “queryField2“: “value” })  – query with multiple criteria

  • Query based on conditions: $gt – > $gte – >=   $lt – <   $lte – <=   $ne – !=

> db.collectionName.find({ “queryField“: {“$gt“: value} })

> db.collectionName.find({ “queryField“: {“$gt“: value, $lt“: value } })

> db.collectionName.find({“queryField“: { “$elemMatch“: {“$gt“: value, $lt“: value } } }) – range queries on an array that has at least 1 element matches the criteria

  • Projections – specifies the exact fields we want to select or exclude; cannot have a mix of inclusion and exclusion

> db.collectionName.find({ “queryfield“: “value” }, { “field“: true, field“: true }) – only retrieve the data needed

> db.collectionName.find({ “queryfield“: “value” }, { “field“: false, field“: false }) – exclude specific fields

> db.collectionName.find({ “queryfield“: “value” }, { “field“: true, _id“: false }) – removing the id field

  • Cursor Methods

> it – iterates the cursor to display the next 20 documents

> db.collectionName.find().count() – returns the count of matching documents

> db.collectionName.find().sort({ “field“: -1/1 }) – sort documents in ascending(1) or descending(-1) order

  • Pagination

> db.collectionName.find().limit(number) – limits pagination to a number of documents

> db.collectionName.find().skip(number).limit(number) – shows the 2nd page

> db.collectionName.find().skip(number*2).limit(number) – shows the 3rd page

  • Referencing Documents

Collection1 = { “collection2_id”: “name” } -> collection2 = { “_id”: “name“}

  • Inserting Referenced Documents

> db.collection1.insert({ “collection2_id”: “name”  }) -> db.collection2.insert({ “_id”: “name”  })

  • Querying a Referenced Documents – 2 queries

> db.collection1.find({ “fieldName“: “value”  }) -> db.collection2.find({ “_id”: “name”  })

  • Data Modeling

Atomic Write Operations – if we update a document and an error occurs on one of the fields, then none of the update will occur

Embedding – allows single query and atomic writes; documents accessed through parent; best if data are always used together; use for less than 100 data; data is never/rarely changed

Referencing – requires 2 queries; documents exist independently; doesn’t support multi-document write; rarely used together; use for more than a hundred-thousand data; data is occasionally/constantly changed

  • Common Aggregations – takes stage operators as parameters; $sum, $avg, $max, $min

> db.collectionName.aggregate( [{ “$group“: { “groupKey“: “$fieldPath“, “accumulators“: {…} }}] ) – stage operator used to group data by any field we specify

When fields begin with a “$”, they are operators that perform a task

When values begin with a “$”, they represent field paths that point to the value

> db.collectionName.aggregate([{ “$group“: { “_id“: “$fieldName“, “fieldName2“: {“$sum”:”$fieldName“}}}])

> db.collectionName.aggregate([ {“$match“: {“field“: “value“}}, {“$group”: {…}} ]) – query that pass documents to the next stage if they meet the specified conditions

  • Aggregation Pipeline

> db.collectionName.aggregate([ {“$match”: {“field“: “value“}}, {“$project”: {“_id”: false, “fieldName“: true }}, {“$group”: {…}}, {“$sort”: {…}}, {“$limit”: 3} ])

Advertisements

About bernadetteengleman

Hello, I am Bernadette from New York City. I moved here from Chicago in 2013. I am originally from the Philippines. I started studying web programming in 2015. I love it because I like making fun websites and web apps. The projects that I built so far are posted below. I like to cook and I love to travel. I also like to volunteer and I am a team leader with the non-profit organization, NYCares.
This entry was posted in MongoDB. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s