Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active November 5, 2017 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilobatistaqueiroz/ce4704f8598d308b1463feb1471820e5 to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/ce4704f8598d308b1463feb1471820e5 to your computer and use it in GitHub Desktop.
MongoDB Tips

MongoDB Tips

Some characteristics

Auto-sharding

Index on any attribute

Conversion/mapping of application objects to database objects not needed.

No complex joins.

BSON documents (A superset of JSON)

BSON stands for "Binary JSON"

BSON extends JSON to provide additional data types, ordered fields, and to be efficient for encoding and decoding.


Terminology

Database | Collection | Document | Field | Default _id | Embedded Documents


Considerations when designing Schema in Mongodb

  1. Combine objects into one document if you will use them together.

  2. Make sure there should not be need of joins.

  3. Duplicate the data: disk space is cheap as compare to compute time.

  4. Do joins while write, not on read.

  5. Do complex aggregation in the schema.


Some commands

use DATABASE_NAME

is used to create database

create a new database if it doesn't exist, otherwise it will return the existing database.

but to effectively create the database, you need to insert at least one document into it.

db

currently selected database

show dbs

list the databases

createCollection

Some options

Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size

Capped is good for logs for example.

db.collection.drop()


data types supported

String, Integer, Boolean, Double, Min/Max keys, Arrays, Timestamp, Object, Null, Symbol, Date, Object Id, Binary data, Code, Regular expression

Bson documents can have types like

string, integer, double, decimal

date (integer number of milliseconds since the Unix epoch)

byte array (binary data)

boolean (true and false)

null, BSON object, BSON array, JavaScript code, MD5 binary data, Regular expression

Inserting documents

db.COLLECTION_NAME.insert(document)

>db.mycol.insert({
   _id: ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no sql database',
   by: 'tutorials point',
   url: 'http://www.tutorialspoint.com',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
})

Save

db.post.save(document)

If you don't specify _id in the document then save() method will work same as insert() method.

If you specify _id then it will replace whole data of document containing _id as specified in save() method.

Finding

db.COLLECTION_NAME.find()

db.mycol.find().pretty()

>db.mycol.find().pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}

findOne()


RDBMS Where clause equivalents

Equality

db.mycol.find({"by":"tutorials point"}).pretty() 
where by = 'tutorials point'

Less Than

db.mycol.find({"likes":{$lt:50}}).pretty()
where likes < 50

geospatial-queries

https://docs.mongodb.com/manual/geospatial-queries/

https://docs.mongodb.com/v3.0/tutorial/geospatial-tutorial/

Aggreggation

Map Reduce

GridFS

Capped Collections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment