Anda di halaman 1dari 4

MongoDB tutorial

MongoDB is an open source document-oriented NoSQL database system. MongoDB is mainly designed for web applications and internet infrastructure. The data model and persistence strategies are built for high read and write throughput and the ability to scale easily with automatic failover. MongoDB is attractive, not because of its scaling strategy, but rather because of its intuitive data model. Given that a document-based data model can represent rich, hierarchical data structures, its often possible to do without the complicated multi-table joins imposed by relational databases. With a document model, by contrast, most of a products information can be represented within a single document. A document is essentially a set of property names and their values. The values can be simple data types, such as strings, numbers, and dates. But these values can also be arrays and even other documents

Getting A Database Connection


By default the shell connects to database 'test' on localhost. To switch databases, type: > use tutorial Youll see a message verifying that youve switched databases. Switching to a database with the use command won't immediately create the database. In fact, creating the database isnt required. Databases and collections are created only when documents are first inserted. This behavior is consistent with MongoDBs dynamic approach to data; just as the structure of documents need not be defined in advance, individual collections and databases can be created at runtime.

Inserting and selecting data


> emp = { name: 'John' }; The command creates emp object (document) with name John. > emp2 = { age: 34 }; The documents we store can have different fields - in this example, the documents have no common data elements. Let's create a employee collection and insert the two documents into it. http://www.learn-with-video-tutorials.com/mongodb-video-tutorial

MongoDB tutorial
> db.employees.save(emp); > db.employees.save(emp2); To save those documents, we need to choose a collection to save it to. We saved it to the employees collection. We did not predefine the collection. The database creates it automatically on the first insert. We can verify that the documents has been saved. > db.employees.find(); The find method returns the inserted document. An _id fields have been added to the documents. We can think of the _id value as the documents primary key. Every document requires this value. > emp3 = { _id: 1, name: 'Emma' }; Were allowed to enter a custom _id as long as we can guarantee its uniqueness. But if we omit the _id field, then a MongoDB object ID will be inserted automatically. > db.employees.save(emp3); > db.employees.find(); Third document has beed added. > db.employees.insert({name: 'Jacob'}); We can create a document and insert it into collection in one command. MongoDB command db.employees.insert({name: 'Jacob'}); is equivalent SQL command INSERT INTO employees(name) VALUES('Jacob'); There should now be four documents in the collection. We can verify this by running the COUNT() command: http://www.learn-with-video-tutorials.com/mongodb-video-tutorial

MongoDB tutorial
> db.employees.count(); > db.employees.find(); MongoDB command db.employees.find(); is equivalent SQL command SELECT * FROM employees We can pass a query selector to the FIND method. A query selector is a document thats used to match against all documents in the collection. To query for all documents where the name is Emma, we should pass a simple document that acts as our query selector: > db.employees.find( { name: 'Emma'} ); The query selector {username: 'Emma'} returns all documents where the name is Emma. SELECT * FROM employees WHERE name = 'EMMA'

Updating data
Update command requires at least two arguments: the first specifies which documents to update, the second defines how the selected documents should be modified. > db.employees.update( {name: 'Emma'}, { $set: { age: 27 } } ); This update tells MongoDB to find a document where the employee name is Emma, and then to set the value of the age property to 27. UPDATE employees SET age=27 WHERE name='Emma' > db.employees.find( { name: 'Emma'} ); Age property has been added. > db.employees.update( { name: 'Emma' }, { $unset: { age: 1 } } ); If we want to delete a age field we should use the statement. http://www.learn-with-video-tutorials.com/mongodb-video-tutorial

MongoDB tutorial
> db.employees.find( { name: 'Emma'} ); There is no age field.

Deleting data
If we want to remove only a certain subset of a collections documents, we can pass a query selector to the REMOVE() method. > db.employees.remove( { name: 'Emma' } ); If you want to remove all employees whose name is Emma, we should use the expression: DELETE FROM employees WHERE name='Emma' > db.employees.find(); There are no documents with Emma name. > db.employees.remove(); > db.employees.find(); If given no parameters, a REMOVE() method will clear a collection of all its documents. > db.employees.drop(); If we want to delete the collection along with all of its indexes, we should use the DROP() method. Watch video lesson, visit http://www.learn-with-video-tutorials.com/mongodb-video-tutorial

http://www.learn-with-video-tutorials.com/mongodb-video-tutorial