230 likes | 363 Views
Overview. Document oriented , not table / row oriented Collection of binary JSON (BSON) documents Schemaless No relations or transactions native in database Scalable and high-performance Full index support Written in C++ Servers for all major platforms
E N D
Overview • Document oriented, nottable/roworiented • Collection of binary JSON (BSON) documents • Schemaless • No relations or transactions native in database • Scalableandhigh-performance • Full index support • Written in C++ • Servers forall major platforms • Drivers forall major development environments • Free and open-source, but also commercial support
NoSQL “Next Generation Databases mostly addressing some of the points: being non-relational, distributed, open-source and horizontally scalable”
BASE As a contrast to ACID • BasicallyAvailablemost data is available most of the time • Soft statethe DB provides a relaxed view of data in terms of consistency • Eventually consistentdata is eventually copied to all applicable nodes, but there is no requirement for all nodes to have identical copies of any given data all the time
Use cases • High performance andscalableapplications • Most web applicationswhereyouwouldpreviouslyuse SQL Do notusefor: • Transaction-criticalapplications
Performance • inserting 50,000 independent objects using NoRM for MongoDB and LINQ to SQL for SQL Server 2008 • five concurrent clients • Queries on indexedidfields
BSON • Binary JSON • Binary encoded serialization of JSON-like documents • Like JSON, BSON supports the embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow representation of data types that are not part of the JSON spec. For example, BSON has a Date type and a BinData type. • The driver performs translation from the language’s “domain object” data representation to BSON, and back
Embeddingdocuments • Nesting of objects and arrays inside a BSON document • For a “contains” type of relationship • Retrieve entire document with one call • { _id: ObjectId(‘12345‘), author: 'joe', created : new Date('03/28/2009'), title : 'Yetanother blog post', text : 'Here is the text...', tags : [ 'example', 'joe' ], comments : [ { author: 'jim', comment: 'I disagree' }, { author: 'nancy', comment: 'Good post' } ] }
Linkingdocuments • “application-level relations” • Whereembeddingwouldcauseduplication of data • { _id: ObjectId(‘12345‘), • author: 'joe', • created : new Date('03/28/2009'), • title : 'Yetanother blog post', • text : 'Here is the text...', • tags : [ 'example', 'joe‘ ] • } • { author: 'jim', • post_id: ObjectId(‘12345‘), • comment: 'I disagree‘ • } • { author: 'nancy', • post_id: ObjectId(‘12345‘), • comment: 'Good post' • }
Querying • Queries return a cursor, which can be iterated to retrieve results • Query optimizer executes new plans in parallel • Queries are expressed as BSON documents which indicate a query pattern db.users.find({'last_name': 'Smith'}) // retrievessn field fordocumentswherelast_name == 'Smith': db.users.find({last_name: 'Smith'}, {'ssn': 1}); // retrieveallfields *except* the thumbnail field, foralldocuments: db.users.find({}, {thumbnail:0}); // retrieveall users order bylast_name: db.users.find({}).sort({last_name: 1}); // skip and limit: db.users.find().skip(20).limit(10);
Advanced querying { name: "Joe", address: { city: "San Francisco", state: "CA" } , likes: [ 'scuba', 'math', 'literature' ] } // field in sub-document: db.persons.find( { "address.state" : "CA" } ) // find in array: db.persons.find( { likes : "math" } ) // regularexpressions: db.persons.find( { name : /acme.*corp/i } ); // javascript where clause: db.persons.find("this.name != 'Joe'"); // check forexistence of field: db.persons.find( { address: { $exists : true } } ); • Aggregatequerieslikegroupby, count, distinct; onlyavailablefor single instances
Inserting& updating • Supports bulk inserts • Default saves are upserts • In place updating • Atomic transactions for single documents • Server side JavaScriptexecution
C# Driver // Opening a server connection; usesconnection pool so no needfordisconnect var connectionString = "mongodb://localhost/?safe=true"; var server = MongoServer.Create(connectionString); // Get a referenceto the “test” database vardatabase = server.GetDatabase("test"); // Get a referenceto the “entities” collection varcollection = database.GetCollection<Entity>("entities"); // Insertinganentity; willset the Idifnecessary varentity = newEntity { Name = "Tom" }; collection.Insert(entity); varid = entity.Id; // Retrieving a single document on primarykey var query = Query.EQ("_id", id); varentity = collection.FindOne(query); // Savinganentity (performsupsert) entity.Name = "Dick"; collection.Save(entity); // Updating anentitydirectly varupdate = Update.Set("Name", "Harry"); collection.Update(query, update); // Delete from database collection.Remove(query);
Indexes • Unique index on primarykey (_idfield) • Create index fromapplication code (ensureIndex) • Index on embeddeddocumentsandfields • Index on array fields (multikey index) • Unique andsparse index • Geospatial index • TTL index • No native full textindexing
Replication • A replica set is a cluster of mongodinstances • 2-12 instances; one is primary • Writesare directedtoprimary • Secondaryinstancesreplicatefromprimaryasynchronously • Automatedfailover; whenprimaryfails a secondarywillbeelected the new primary
Auto-sharding • Partitions data acrossshards • Any BSON document resides on onlyoneshard • Increaseswritecapacityandtotal data size • Data automaticallydistributed • Shardingtransparenttoapplicationlayer • Partitioningbased on client-definedshardkey • Goodshardkeys are highlydistributed in valueandwrite operations • Shardingrequiresconfig servers (minimal 3) tomaintainmetadata
GridFS • Large blob data, limitedonlyby storage space • BSON documents max 16 MB • Supports manythousands of files • Supports oftenchanging files