27
{ MongoDB “by”: “JayaNaresh”, “e-mail”: “[email protected]}

Mongo DB Presentation

Embed Size (px)

Citation preview

Page 1: Mongo DB Presentation

{MongoDB

“by”: “JayaNaresh”,

“e-mail”: “[email protected]” }

Page 2: Mongo DB Presentation

MongoDB (derived from "humongous") is a cross-platform document-oriented database system. Classified as a NoSQL database, MongoDB avoids the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (Binary-encoded JSON representation), making the integration of data in certain types of applications easier and faster.

First developed by 10gen (now MongoDB Inc.) in October 2007 as a component of a planned platform as a service product, the company shifted to an open source development model in 2009, with 10gen offering commercial support and other services. Since then, MongoDB has been adopted as backend software by a number of major websites and services, including Craigslist, eBay, Foursquare, SourceForge, and The New York Times, among others. MongoDB is the most popular NoSQL database system.

A Brief History…

Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open source software.

Page 3: Mongo DB Presentation

NoSQL NoSQL is intended as shorthand for "not only SQL." Complete architectures almost always mix traditional and NoSQL databases.

Brewer's (CAP) Theorem

It's impossible for a distributed computer system to simultaneously provide all three of these guarantees:

• Consistency (all nodes see the same data at the

same time)

• Availability (node failures don't prevent

survivors from continuing to operate)

• Partition tolerance (no failures less than total

network failures cause the system to fail)

*NoSQL's primary goal is to achieve horizontal scalability. It attains this by reducing transactional semantics and referential integrity.

Page 4: Mongo DB Presentation

JSON JavaScript Object Notation

JSON is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

• A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

• An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

*Complex Objects in JSON

Page 5: Mongo DB Presentation

JSON Objects in JSON

• Simple JSON Object:{ "firstName":"Jaya Naresh","lastName": ”k","numberOfPosts":10

}

• Complex JSON Object:{"firstName": "Jaya Naresh","lastName": ”K","numberOfPosts": 10,"friends": [

{"name": "Siva","city": ”Vijayawada","posts": 20

}, {"name": "Ram","city": "Hyderabad","posts": 15

},{"name": "Lakshman","city": "Hyderabad","posts": 5

}]

}

• With Arrays:{"firstName": "Jaya Naresh","lastName": ”K","numberOfPosts": 10,"tags": [

"Music”, "Movies”, "Gadgets”

]}

Page 6: Mongo DB Presentation

MongoDB

RDBMS

• Databases

• Tables

• Rows

Schema free: No

MongoDB

• Databases

• Collections

• Documents

Schema free: Yes, Dynamic Schema

Page 7: Mongo DB Presentation

MongoDBTraditional RDBMS Table(s) structure

TablesMaster tables

Relationships

• One-to-One• One-to-Many• Many-to-One• Many-to-Many

Page 8: Mongo DB Presentation

MongoDBCollection(s) Structure in MongoDB

• Embedded

{"id": 1,"name": "Jaya Naresh K","department": [{"id": "D001","name": ”Tech"

}],"location": [{"id": "L001","name": ”SFO"

},{"id": "L002","name": "Hyderabad"

}]

}

*embedded sample

• Referenced

Page 9: Mongo DB Presentation

Naming Rules

DatabasesDatabase names cannot contain “.”, “$”, or “\0” (the null character). Names can only contain characters that can be used on your file system as filenames. Admin, config, and local are reserved database names (you can store your own data in them, but you should never drop them).

CollectionsCollection names cannot contain “$” or “\0”. Names prefixed with “system.” are reserved by MongoDB and cannot be dropped (even if you created the collection). Dots are often used for organization in collection names, but they have no semantic importance. A collection named “che.se” has no more relation to a collection named “che” than one named “cheese” does.

Field NamesField names cannot contain “.” nor “\0”. Fields should only contain “$” when they are database references.

Page 10: Mongo DB Presentation

Insert Documents

• Save / Insert Function

Both functions does the same functionality

db.collection.save(<document>)db.collection.insert(<document>)

Commanddb.books.save( { item: "Divine Comedy”, price: 18, stock: 5 } )

db.books.insert({”_id": 1,"name": "Jaya Naresh K","department": [

{"id": "D001","name": “Tech."

}]

})

• The _id FieldThe _id field has the following behavior and constraints:

1. In documents, the _id field is always indexed for regular collections.2. The _id field may contain values of any BSON data type, other than an array.

Page 11: Mongo DB Presentation

Query Operators

• Query Format

Queries are generally of the form (JSON):

{key : value} or {key : {$op : value}}

For example

{age : 18} or {age : {$gte : 18}}

db.collection.find({age : 18}) or db.collection.findOne({age : 18})

There are three exceptions to this rule: $and, $or, and $nor, which are all top-level:

{$or : [{age: {$gte : 18}}, {age : {$lt : 18}]}

{$and : [{age: 18}}, {age : 19}]},

• Dot Notation

MongoDB uses the dot notation to access the elements of an array and to access the fields of a subdocument.

To access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:

'<array>.<index>’

To access a field of a subdocument with dot-notation, concatenate the subdocument name with the dot (.) and the field name, and enclose in quotes:

'<subdocument>.<field>’

Page 12: Mongo DB Presentation

Query Operators

• Select All Documents in a Collection

An empty query document ({}) selects all documents in the collection:

db.inventory.find( {} )

• Specify Equality Condition

To specify equality condition, use the query document { <field>: <value> } to select all documents that contain the<field> with the specified <value>.The following example retrieves from the inventory collection all documents where the type field has the value snacks:

db.inventory.find( { type: "snacks" } )

• Specify Conditions Using Query Operators

A query document can use the query operators to specify conditions in a MongoDB query.The following example selects all documents in the inventory collection where the value of the type field is either 'food' or 'snacks':

db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )

Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

Page 13: Mongo DB Presentation

Query Operators• Specify AND Conditions

A compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions. In the following example, the query document specifies an equality match on the field food and a less than ($lt) comparison match on the field price:

db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )

• Specify OR Conditions

Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition. In the following example, the query document selects all documents in the collection where the field qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:

db.inventory.find({ $or: [

{ qty: { $gt: 100 } },{ price: { $lt: 9.95 } }

]}

)

• Specify AND as well as OR Conditions

With additional clauses, you can specify precise conditions for matching documents.In the following example, the compound query document selects all documents in the collection where the value of the type field is 'food' and either the qty has a value greater than ($gt) 100 or the value of the price field is less than ($lt) 9.95:

db.inventory.find( { type: 'food', $or: [ { qty: { $gt: 100 } },{ price: { $lt: 9.95 } } ]

} )

Page 14: Mongo DB Presentation

Query Operators

Subdocuments

When the field holds an embedded document (i.e. subdocument), you can either specify the entire subdocument as the value of a field, or “reach into” the subdocument using dot notation, to specify values for individual fields in the subdocument:

• Exact Match on Subdocument

To specify an equality match on the whole subdocument, use the query document { <field>: <value> } where <value> is the subdocument to match. Equality matches on a subdocument require that the subdocument field match exactly the specified<value>, including the field order. In the following example, the query matches all documents where the value of the field producer is a subdocument that containsonly the field company with the value 'ABC123' and the field address with the value '123 Street', in the exact order:

db.inventory.find({producer: {

company: 'ABC123',address: '123 Street'

}}

)

• Equality Match on Fields within Subdocument

Equality matches for specific fields within subdocuments select the documents in the collection when the field in the subdocument contains a field that matches the specified value. In the following example, the query uses the dot notation to match all documents where the value of the field producer is a subdocument that contains a field company with the value 'ABC123' and may contain other fields:

db.inventory.find( { 'producer.company': 'ABC123' } )

Page 15: Mongo DB Presentation

Query Operators

Arrays

When the field holds an array, you can query for an exact array match or for specific values in the array. If the array holdssub-documents, you can query for specific fields within the sub-documents using dot notation:

• Exact Match on an Array

To specify equality match on an array, use the query document { <field>: <value> } where <value> is the array to match. Equality matches on the array require that the array field match exactly the specified <value>, including the element order.In the following example, the query matches all documents where the value of the field tags is an array that holds exactly three elements, 'fruit', 'food', and 'citrus', in this order:

db.inventory.find( { tags: [ 'fruit', 'food', 'citrus' ] } )

• Match an Array Element

Equality matches can specify a single element in the array to match. These specifications match if the array contains at least oneelement with the specified value.In the following example, the query matches all documents where the value of the field tags is an array that contains 'fruit'as one of its elements:

db.inventory.find( { tags: 'fruit' } )

• Match a Specific Element of an Array

Equality matches can specify equality matches for an element at a particular index or position of the array.In the following example, the query uses the dot notation to match all documents where the value of the tags field is an array whose first element equals 'fruit':

db.inventory.find( { 'tags.0' : 'fruit' } )

Page 16: Mongo DB Presentation

Query Operators

Query Selectors

• Comparison

Name Description$gt Matches values that are greater than the value specified in the query.$gte Matches values that are equal to or greater than the value specified in the query.$in Matches any of the values that exist in an array specified in the query.$lt Matches values that are less than the value specified in the query.$lte Matches values that are less than or equal to the value specified in the query.$ne Matches all values that are not equal to the value specified in the query.$nin Matches values that do not exist in an array specified to the query.

• Logical

Name Description$or Joins query clauses with a logical OR returns all documents that match the conditions of either

clause.$and Joins query clauses with a logical AND returns all documents that match the conditions of both

clauses.$not Inverts the effect of a query expression and returns documents that do not match the query

expression.$nor Joins query clauses with a logical NOR returns all documents that fail to match both clauses.

Page 17: Mongo DB Presentation

Query Operators and Projection Operators

Query Selectors

• Evaluation

Name Description$mod Performs a modulo operation on the value of a field and selects documents with a

specified result.$regex Selects documents where values match a specified regular expression.$where Matches documents that satisfy a JavaScript expression.

• Array

Name Description$all Matches arrays that contain all elements specified in the query.$elemMatch Selects documents if element in the array field matches all the specified

$elemMatch condition.$size Returns documents if the array field is a specified size.

Page 18: Mongo DB Presentation

Query Operators and Projection Operators

Query Selectors

• Geospatial

Name Description$geoWithin Selects geometries within a bounding GeoJSON geometry.$geoIntersects Selects geometries that intersect with a GeoJSON geometry.$near eturns geospatial objects in proximity to a point.$nearSphere Returns geospatial objects in proximity to a point on a sphere.

• Projection Operators

Name Description$ Projects the first element in an array that matches the query condition.$elemMatch Projects only the first element from an array that matches the specified $elemMatch

condition.$slice Limits the number of elements projected from an array.

Supports skip and limit slices.

Page 19: Mongo DB Presentation

Update Documents

• Update Operators

Fields

Name Description$inc Increments the value of the field by the specified amount.$rename Renames a field.$setOnInsert Sets the value of a field upon document creation during an upsert. Has no effect on

update operations that modify existing documents.$set Sets the value of a field in an existing document.$unset Removes the specified field from an existing document.

Page 20: Mongo DB Presentation

Update Documents

• Update Functiondb.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean> } )

Sampledb.books.update( { item: "Divine Comedy" }, { $set: { price: 18 }, $inc: { stock: 5 } } )

• Add New FieldsIf the <update> parameter contains fields not currently in the document, the update() method adds the new fields to the document. The following operation adds two new fields: mbranch and aka. The operation adds aka in the subdocument name:

db.bios.update( { _id: 3 }, { $set: { mbranch: "Navy”, "name.aka": "Amazing Grace” } } )

• Remove FieldsThe following operation uses the $unset operator to remove the birth field from the document that has _id equal to 3:

db.bios.update( { _id: 3 }, { $unset: { birth: 1 } } )

Page 21: Mongo DB Presentation

Update Documents

• Insert a New Document if No Match Exists (Upsert)The following command sets the upsert option to true so that update() creates a new document in the books collection if no document matches the <query> parameter:

db.books.update( { item: "The New Life" },{ item: "The New Life",author: "Dante",price: 15 },

{ upsert: true })

• Update Multiple DocumentsTo update multiple documents, set the multi option to true. The following example queries the bios collection for all documents where awards.award is set to Turing. The update sets the turing field to true:

db.bios.update({ "awards.award": "Turing" },{ $set: { turing: true } },{ multi: true }

)

• Combine the Upsert and Multi Parametersdb.books.update( { author: "Dante" },

{ $set: { born: "Florence", died: "Ravenna" } },{ upsert: true, multi: true }

)

Page 22: Mongo DB Presentation

Update Arrays

• Operators

Name Description$ Acts as a placeholder to update the first element that matches the query condition in an update.$addToSet Adds elements to an existing array only if they do not already exist in the set.$pop Removes the first or last item of an array.$pullAll Removes all matching values from an array.$pull Removes items from an array that match a query statement.$pushAll Deprecated. Adds several items to an array.$push Adds an item to an array.

• ModifiersName Description$each Modifies the $push and $addToSet operators to append multiple items for array updates.$slice Modifies the $push operator to limit the size of updated arrays.$sort Modifies the $push operator to reorder documents stored in an array.

More @ http://docs.mongodb.org/manual/reference/operator/update/

Page 23: Mongo DB Presentation

Update Arrays

• Update an Element by Position

If the update operation requires an update of an element in an array field, the update() method can perform the update using the position of the element and dot notation. Arrays in MongoDB are zero-based.

The following operation queries the bios collection for the first document with _id field equal to 1 and updates the second element in the contribs array:

db.bios.update({ _id: 1 },{ $set: { "contribs.1": "ALGOL 58" } }

)

• Update an Element if Position is Unknown

If the position in the array is not known, the update() method can perform the update using the $ positional operator. The array field must appear in the <query> parameter in order to determine which array element to update.

The following operation queries the bios collection for the first document where the _id field equals 3 and the contribs array contains an element equal to compiler. If found, the update() method updates the first matching element in the array to A compiler in the document:

db.bios.update({ _id: 3, "contribs": "compiler" },{ $set: { "contribs.$": "A compiler" } }

)

Page 24: Mongo DB Presentation

Update Arrays

• Update a Document Element

The update() method can perform the update of an array that contains subdocuments by using the positional operator (i.e. $) and the dot notation.

The following operation queries the bios collection for the first document where the _id field equals 6 and the awards array contains a subdocument element with the by field equal to ACM. If found, the update() method updates the by field in the first matching subdocument:

db.bios.update({ _id: 6, "awards.by": "ACM" } ,{ $set: { "awards.$.by": "Association for Computing Machinery" } }

)

• Add an Element

The following operation queries the bios collection for the first document that has an _id field equal to 1 and adds a new element to the awards field:

db.bios.update({ _id: 1 },{

$push: { awards: { award: "IBM Fellow", year: 1963, by: "IBM" } }}

)

Page 25: Mongo DB Presentation

Mongo Shell Methods

Collection Functions

Name Description

db.collection.aggregate() Provides access to the aggregation pipeline.

db.collection.distinct() Returns an array of documents that have distinct values for the specified field.

db.collection.drop() Removes the specified collection from the database.

db.collection.dropIndex() Removes a specified index on a collection.

db.collection.dropIndexes() Removes all indexes on a collection.

db.collection.ensureIndex() Creates an index if it does not currently exist. If the index existsensureIndex() does

nothing.

db.collection.find() Performs a query on a collection and returns a cursor object.

db.collection.findAndModify() Atomically modifies and returns a single document.

db.collection.findOne() Performs a query and returns a single document.

db.collection.getIndexes() Returns an array of documents that describe the existing indexes on a collection.

db.collection.group() Provides simple data aggregation function. Groups documents in a collection by a key, and

processes the results. Use aggregate() for more complex data aggregation.

db.collection.insert() Creates a new document in a collection.

db.collection.mapReduce() erforms map-reduce style data aggregation.

db.collection.reIndex() Rebuilds all existing indexes on a collection.

db.collection.remove() Deletes documents from a collection.

db.collection.renameCollection() Changes the name of a collection.

db.collection.save() Provides a wrapper around an insert() and update() to insert new documents.

db.collection.update() Modifies a document in a collection.

JavaScript in MongoDB Although these methods use JavaScript, most interactions with MongoDB do not use JavaScript but use an idiomatic driver in the language of the interacting application.

Page 26: Mongo DB Presentation

Mongo Shell Methods

Cursor Functions

Name Description

cursor.count() Returns a count of the documents in a cursor.

cursor.explain() Reports on the query execution plan, including index use, for a cursor.

cursor.hint() Forces MongoDB to use a specific index for a query.

cursor.limit() Constrains the size of a cursor’s result set.

cursor.map() Applies a function to each document in a cursor and collects the return values in an array.

cursor.max() Specifies an exclusive upper index bound for a cursor. For use with cursor.hint()

cursor.min() Specifies an inclusive lower index bound for a cursor. For use with cursor.hint()

cursor.next() Returns the next document in a cursor.

cursor.size() Returns a count of the documents in the cursor after applying skip() and limit()methods.

cursor.skip() Returns a cursor that begins returning results only after passing or skipping a number

of documents.

cursor.sort() Returns results ordered according to a sort specification.

cursor.toArray() Returns an array that contains all documents returned by the cursor.

More shell functions @ http://docs.mongodb.org/manual/reference/method/

Page 27: Mongo DB Presentation

RefCardz