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.
Database | Collection | Document | Field | Default _id | Embedded Documents
-
Combine objects into one document if you will use them together.
-
Make sure there should not be need of joins.
-
Duplicate the data: disk space is cheap as compare to compute time.
-
Do joins while write, not on read.
-
Do complex aggregation in the schema.
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()
String, Integer, Boolean, Double, Min/Max keys, Arrays, Timestamp, Object, Null, Symbol, Date, Object Id, Binary data, Code, Regular expression
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
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
})
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.
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()
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