Nosql- MongoDB
Introduction: this part of content we can have more practice on ec2 mongo
-
– https://docs.mongodb.com/v3.3/tutorial/iterate-a-cursor/
-
– https://docs.mongodb.com/v3.4/reference/method/js-
cursor/
The Points of abstraction:
- Manage JSON document
- Key concepts: document, collection, primary key (_id)
- Query language: insert, find, update, remove, aggregate
- Sharding
Document store
MongoDB is a document database
A document is similar to an json object
- Consists of field-value pairs
- Value may be another document, array, String, number
Document = record/row in RDBMS
Databases
No need to explicitly create it, just use it
automatically created once add a collection (i.e. table) to it
use inf551
show databases
db.createCollection('person')
Collections
Document are stored in a collection = table in RDBMS
But docs may have different structure, while records in RDBMS have the same schema
Primary key
Every doc has a unique_id field
ObjectId() function-- creates an ID
db.person.insert({"_id": ObjectId(), "name": "john smith"})
db.person.insert({"name": "john smith"})
no specification of "_id" field.
But an id will be automatically created
A 12-byte hexademical value
e.g.
> db.person.find()
{ "_id" : 1, "name" : "john smith" }
{ "_id" : ObjectId("590172335afab943f9877543"), "name" : "john smith" }
{ "_id" : ObjectId("5901723a5afab943f9877544"), "name" : "john smith" }
Embedded sub-document
db.person.find()
{ "_id" : 1, "name" : "john smith" }
{ "_id" : ObjectId("590172335afab943f9877543"), "name" : "john smith" }
{ "_id" : ObjectId("5901723a5afab943f9877544"), "name" : "john smith" }
{ "_id" : ObjectId("5901729c5afab943f9877545"), "name" : "david johnson",
"address" : { "street" : "123 maple", "city" : "LA", "zip" : 91989 }, "phone" : [ "323-123-0000", "626-124-0999" ] }
Query
db.person.find() —Return all documents in person
db.person.find({"name": "kevin small"})
db.person.find({"age": {$gt: 25}}) => “>”
db.person.find({"name": "kevin small", "age": {$gt: 25}}) =>”and”
db.person.find({ $or: [{"name": "kevin small"}, {"age": {$gt: 25}} ] }) =>”or”
db.person.find({ $or: [{"name": "kevin small"}, {"age": {$gt: 25}} ] }) =>”or”
Query operator
Introduced by $
$lt, $gt(>), $lte, $gte, $ne(!=) —comparison
$or, $and, $not—logical
Projection
db.person.find( {"age": {$ne: 25} },
{"name":1, "age": 1} )
'select _id, name, age from users where age != 25'
1: included in result; 0: do not
db.person.find( {"age": {$ne: 25} },
{"name":1, "age": 1, "_id": 0} )
not return id, e.g.
{ "name" : "john smith" }
{ "name" : "david johnson" }
{ "name" : "kevin small", "age" : 35 }
{ "name" : "kevin small", "age" : 35 }
Update
db.person.update({ "age": { $gt: 25 } }, { $set: { "status": "C" } }, { multi: true } )
Update users set status = 'C' where age > 25
- Existing documents may not have status field; if not, insert it instead
- Update one or all documents
db.person.update({}, {$set: {"status":'C'}}, {multi: true})
Add "status" field to all documents
Remove
db.person.update({}, {$unset: {"status": ""}}, {multi: true})
Remove "status" field from all docs
Delete
db.person.remove({}) – Remove all documents
db.person.remove( { "age": {$gt: 30} } ) -condition
count
db.person.count()
Embedded document
db.person.find({"address.city": "LA"})
Return all docs whose city sub-field of address field = "LA"
Aggregation
db.person.aggregate([{"$group": { _id: "$age", total:{$sum:1} } } ])
{ "_id" : 25, "total" : 1 }
{ "_id" : 35, "total" : 1 }
{ "_id" : null, "total" : 4 }
_id = age grouped attribute
Sharding-horizontal scaling
At collection level—Method of distributing data across multi-machines
User-specified shard key, a field in a document
Support sharing by key range or hashing
评论
发表评论