3. There is no create database command in MongoDB. Actually, MongoDB do not provide any
command to create database.
It may be look like a weird concept, if you are from traditional SQL background where you need
to create a database, table and insert values in the table manually.
Here, in MongoDB you don't need to create a database manually because MongoDB will create
it automatically when you save the value into the defined collection at first time.
5. Here, your created database “mydb" is not present in the list, insert at least one document into
it to display database:
>db.mycollections.insert({"name":“me"})
WriteResult({ "nInserted": 1})
>show dbs
mydb 0.078GB
local 0.078GB
6. In MongoDB, db.collection.drop() method is used to drop a collection from a database. It
completely removes a collection from the database and does not leave any indexes associated
with the dropped collections.
The db.collection.drop() method does not take any argument and produce an error when it is
called with an argument. This method removes all the indexes associated with the dropped
collection.
Syntax:
db.COLLECTION_NAME.drop()
7. First check the already existing collections in your database.
>use mydb
Switched to db mydb
> show collections
SSSIT
system.indexes
db.dropDatabase()
Dropped mydb
9. Create collection and drop collection
CREATE COLLECTION
db.createCollection(“mycollection”)
show collections
mycollection
db.mycollection2.insert({“name”:”max”})
DROP COLLECTION
db.mycollection.drop()
11. MongoDB insert documents
In MongoDB, the db.collection.insert() method is used to add or insert new documents into a
collection in your database.
Syntax
>db.COLLECTION_NAME.insert(document)
14. Query document
use school
db.students.find()
{ "_id" : ObjectId(5983548781331adf45ec5), “1":“mark"}
{ "_id" : ObjectId(5983548781331adf45ec6), “2":“max"}
* all data
db.students.find().pretty()
*Object id,studentno student name
15. db.students.findone()
* gives first document
db.students.find(“studentno”:”1”)
*returns student 1 document
db.student.find(“age”:{$gt :”15”})
* returns age greater than 15
db.student.find(“age”:{$gte :”15”})
* returns age greater than equal to 15
17. MongoDB's update() and save() methods are used to update document into a collection. The
update() method updates the values in the existing document while the save() method replaces
the existing document with the document passed in save() method.
MongoDB Update() Method
The update() method updates the values in the existing document.
Syntax
The basic syntax of update() method is as follows −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
19. By default, MongoDB will update only a single document. To update multiple documents, you
need to set a parameter 'multi' to true.
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
20. MongoDB Save() Method
The save() method replaces the existing document with the new document passed in the save()
method.
Syntax
The basic syntax of MongoDB save() method is shown below −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
21. MongoDB - Delete Document
The remove() Method
MongoDB's remove() method is used to remove a document from the collection.
remove() method accepts two parameters.
One is deletion criteria and second is justOne flag.
deletion criteria − (Optional) deletion criteria according to documents will be removed.
justOne − (Optional) if set to true or 1, then remove only one document.
Syntax
Basic syntax of remove() method is as follows −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
22. Example
Consider the mycol collection has the following data.
{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"},
{_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"},
{_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials Point Overview"}
>db.mycol.remove({'title':'MongoDB Overview'})
WriteResult({"nRemoved" : 1})
> db.mycol.find()
{"_id" : ObjectId("507f191e810c19729de860e2"), "title" : "NoSQL Overview" }
{"_id" : ObjectId("507f191e810c19729de860e3"), "title" : "Tutorials Point Overview" }
23. Remove Only One
If there are multiple records and you want to delete only the first record, then set justOne
parameter in remove() method.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
24. Remove All Documents
If you don't specify deletion criteria, then MongoDB will delete whole documents from the
collection. This is equivalent of SQL's truncate command.
> db.mycol.remove({})
WriteResult({ "nRemoved" : 2 })
> db.mycol.find()
>
25. Cassandra
Apache Cassandra is an open source, distributed and decentralized/distributed storage
system (database), for managing very large amounts of structured data spread out across the
world.
It provides highly available service with no single point of failure.
It is scalable, fault-tolerant, and consistent.
Apache Cassandra is a highly scalable, high-performance distributed database designed to
handle large amounts of data across many commodity servers, providing high availability with
no single point of failure.
It is a type of NoSQL database.
26. Features of Cassandra
Cassandra has become so popular because of its outstanding technical features. Given below are some of the features of
Cassandra:
Elastic scalability − Cassandra is highly scalable; it allows to add more hardware to accommodate more customers and
more data as per requirement.
Always on architecture − Cassandra has no single point of failure and it is continuously available for business-critical
applications that cannot afford a failure.
Fast linear-scale performance − Cassandra is linearly scalable, i.e., it increases your throughput as you increase the
number of nodes in the cluster. Therefore it maintains a quick response time.
Flexible data storage − Cassandra accommodates all possible data formats including: structured, semi-structured, and
unstructured. It can dynamically accommodate changes to your data structures according to your need.
Easy data distribution − Cassandra provides the flexibility to distribute data where you need by replicating data across
multiple data centers.
Transaction support − Cassandra supports properties like Atomicity, Consistency, Isolation, and Durability (ACID).
Fast writes − Cassandra was designed to run on cheap commodity hardware. It performs blazingly fast writes and can
store hundreds of terabytes of data, without sacrificing the read efficiency.
27. History of Cassandra
Cassandra was developed at Facebook for inbox search.
It was open-sourced by Facebook in July 2008.
Cassandra was accepted into Apache Incubator in March 2009.
It was made an Apache top-level project since February 2010.
28. Hbase
HBase is a data model that is similar to Google’s big table designed to provide quick random
access to huge amounts of structured data.