Mongo DB - NoSQL

What is Mongo DB




MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++.
MongoDB is a cross-platformdocument-oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document.

Database

The database is a physical container for collections

Collection

A collection is a group of MongoDB documents. It is the equivalent of an RDBMS table

Document

A document is a set of key-value pairs. Documents have a dynamic schema. Documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.


_id Field of a Document

_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you don’t provide then MongoDB provides a unique id for every document.
These 12 bytes are divided:
first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of MongoDB server and remaining 3 bytes are simple incremental VALUE.


Create DB
  use DATABASE_NAME  
The command will create a new database if it doesn't exist, otherwise, it will return the existing database.

Drop Database
 db.dropDatabase() 
To drop an existing database

Create Collection
 db.createCollection(name, options) 
Used to create a collection

Drop Collection
db.COLLECTION_NAME.drop()
Used to drop a collection

Insert a Document 


db.collection1.insert({
   _id: ObjectId(12345abceert),
   title: 'MongoDB sample, 
   description: 'No SQL db MongoDB ',
   by: 'tech trends',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 200
})

Here collection1 is the collection nameIf the collection doesn't exist in the database, then MongoDB will create this collection and then insert a document into it. 



The find() Method
You need to use MongoDB's find() method to query data from MongoDB collection, 

db.COLLECTION_NAME.find()


AND in MongoDB 


Following example will show all the books written by 'John Kelvin' and whose title is 'MongoDB sample'.

>db.collection1.find({$and:[{"by":"John Kelvin"},{"title": "MongoDB Overview"}]}).pretty() {
   "_id": ObjectId(1wf78ad8902c),
   "title": "MongoDB sample", 
   "description": "MongoDB is no sql database",
   "by": "John Kelvin",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "200"
}


OR in MongoDB 

Following example will show all the tutorials written by 'John Kelvin' or whose title is 'MongoDB sample'.

>db.collection1.find({$or:[{"by":"John Kelvin"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(1wf78ad8902c),
   "title": "MongoDB sample", 
   "description": "MongoDB is no sql database",
   "by": "John Kelvin",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "200"
}




Comments

Popular posts from this blog

How Javascript is working-Mechanisms

Creating a REACT component