Summarise from https://www.tutorialspoint.com/mongodb
- Server / client - mongod / mongo
- Database ("test" the default)
- physical container for collections
- get own set of files on file system
- single server -> many dbs
- Collection (like a RDBMS table)
- group of documents
- exists within a single db
- do not enforce a schema
- can have different fields
- usually all docs in one collection have similar or related purposes
- Document (like a RDBMS row)
- set of key/value pairs (columns)
- have dynamic schema
- docs in same collection can have different set of fields and structure
- common fields in docs can hold different type of data
- embedded documents (RDBMS table join)
- Primary key - default key _id provided by mongoDb
- _id:ObjectId(xxxxxxxxxxxx) - 12 bytes hexadecimal number, unique for every doc (4 bytes timestamp, 3 machine id, 2 process id, 3 serial)
- JSON / BSON
https://halls-of-valhalla.org/beta/articles/the-pros-and-cons-of-mongodb,45/
Pros:
- schema-less
- no complex joins
- deep query-ability, document-based query language
- no ORM
- easy sharding (partitioning)
Cons:
- No joins
- No schema - store key names in memory - memory usage high - slow query
- Concurrency issues
- Young
- Transactions - manual
Start/stop: sudo service mongod start/stop
Shell: mongo
Help: db.help()
In same collection data has a flexible schema.
Considerations:
- Combine objects into one document if they are used together
- Otherwise separate objects but make sure they should not need joins
- Duplicate data (be careful!) as disk space is cheaper than compute time
- Do joins while write / not on read
- Optimise for most frequent use cases
- Do complex aggregation in schema
create or return to: use DB_NAME
check current db: db
list db (need at least one document): show dbs
use db.[collection].insert() to insert document
drop selected db: db.dropDatabase()
db.createCollectin(name, options) - options are optional
- capped - fixed sized collection, automatically overwrite oldest entries
- autoIndexed - automatically create _id, default false
>db.createCollection("mycol", { capped : true, autoIndexId : true, size :
6142800, max : 10000 } )
drop: db.COLLECTION_NAME.drop()
list: show collections
- String - UTF8
- Integer - 32/64bit depending upon server
- Boolean
- Double
- Min/Max keys - compare a value against the lowest/highest BSON elements
- Arrays
- Timestamp
- Object - for embedded documents
- Null
- Symbol - used identically to a String, however reserved for languages that use a specific symbol type
- Date - used to store UNIX time format, can specify own data time by creating object of data
- Object ID
- Binary data
- Code - javascript code
- 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
})
(if COLLECTION not exist, will be created)
Can pass in an array of documents in insert() command
db.post.save(document)
- if no _id then save() will work same as insert()
- if specify _id it replace whole data of document
db.COLLECTION_NAME.find()
- prints all documents in non-structured way
db.COLLECTION_NAME.find().pretty()
- prints in formatted way
db.COLLECTION_NAME.findOne() - return just one
The above accepts JSON objects as conditon, see: https://www.tutorialspoint.com/mongodb/mongodb_query_document.htm
not a pretty language as SQL, best use some visual tools
Update method accepts SELECTION criteria (JSON) and UPDATED DATA criteria (JSON), will update a single record only unless specified {multi:true}
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
save() method replaces existing document with new one
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
remove()
>db.mycol.remove({'title':'MongoDB Overview'}) // remove all selected
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1) // remove just one, or the first
>db.mycol.remove() // remove all
Projection (select partial results, sort of results)
>db.mycol.find({},{"title":1,_id:0}) // select title but hide _id
>db.COLLECTION_NAME.find().limit(NUMBER) // limit number of documents
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) // skip a number of documents
>db.COLLECTION_NAME.find().sort({KEY:1}) // sort, 1 ascending (default) -1 descending
>db.mycol.ensureIndex({"title":1,"description":-1}) // index key by ascending / descending order
More options:
- background - build in background so not blocking activity
- unique
- name - name of index
- dropDups - drop second and more documents when creating index
- sparse ??
- expireAfterSeconds - TTL, control how long DB retains documents in this collection
- v - index version, default depends on DB version
- weights - significant of field relative to other indexed fields (??)
- default_language - string, relate to stop words and rules for stemmer and tokenizer. for text index
- language_override - name of the field in the document that contains the language (to override default language)
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
- $sum : value - sum up value (value can be $field_name, for example)
- $avg
- $min / $max
- $push - insert value to an array in the resulting document
- $addToSet - insert to a set
- $first / $last