Search

androidtour

Category

MongoDB

MongoDB Basic Command with Screenshot

ice_screenshot_20151226-203545

 

Command for Show Collection

ice_screenshot_20151226-203918ice_screenshot_20151226-203600

Advertisements

[MongoDB Tutorial] Using Map Reduce to Manipulate Results of Request from the shell.

Download Example

To understand map reduce operation on a collection to generate specific results.

ice_screenshot_20151228-201639

 

[MongoDB Tutorial] Generating Aggregated Data from a Collection.

Download Example

To understand aggregate operations to retrieve aggregated data sets from a collection.

 

[MongoDB Tutorial] Grouping Objects by Key Values.

Download Example

To understand grouping document together by field values.

ice_screenshot_20151228-185841

[MongoDB Example] Extra Example

Download Example(make_word)

ice_screenshot_20151228-184239

Download Example(doc_add_error)

ice_screenshot_20151228-184214

[MongoDB Tutorial] Deleting Documents from a Collection.

Download Example

To understand hoe to delete single and multiple document from collection.

ice_screenshot_20151228-183613

 

[MongoDB Tutorial] Upserting Documents in a Collection.

Download Example

In this example we will see how upsert (insert+update) is work.

ice_screenshot_20151228-183250

[MongoDB Tutorial] Saving Documents in a Collection.

Download Example

To understand saving new and existing documents.

We save new document in existing category name is “blue” and second is create new document.

ice_screenshot_20151228-181802

[MongoDB Tutorial] Updating Documents in a Collection

Download Example

To understand how updating single document as well as group of documents that match a query.

ice_screenshot_20151228-180732

[MongoDB Tutorial] Inserting Documents into a existing Collection.

Download Example

Inserting single document as well as array of document.

For Remove
//wordsColl.remove({word: {$in: [‘tweet’,’google’, ‘selfie’]}});

ice_screenshot_20151228-022950

[MongoDB Tutorial] Retrieving Distinct Values for a Specific Field.

Download Example

To understand ways to generate lists of distinct field values in collections.

We can categories distinct document based on our condition.

ice_screenshot_20151228-020237

 

[MongoDB Tutorial] Paging Through Documents in a Collection.

Download Example

To understand paging through a series of documents in a large data set.

here as per our data total 10 page are there.

ice_screenshot_20151228-015830

 

[MongoDB Tutorial] Limiting Which Document Fields Are Returned from record

Download Example

To Understand which fields are returned in documents using find operations.

ice_screenshot_20151228-015029

 

[MongoDB Tutorial] Limiting Documents Represented by a Cursor.

Download Example

Example to Demonstrate limiting the number of documents returned from find operation.

ice_screenshot_20151228-013242

[MongoDB Tutorial] Sorting Documents Represented by a Cursor.

Download Example

In this Example we sort the Document based on Requirement.

ice_screenshot_20151228-010359

[MongoDB Tutorial] Counting Document Represented by Cursor

Download Example

This Example finds the Number of Document which is store in Cursor.

Using this Method : cursorName.count()

ice_screenshot_20151226-210120

[MongoDB Tutorial] Retrieving Specific Document

Download Example

ice_screenshot_20151226-204704

 

[MongoDB Tutorial] Retrieving Multiple Document

Download Example

In this Example there are total four method to retrieve Document.

1. Using Each List

2. Using Mapped Array

3. Using Indexed Document in Array

4. Using Next Document from Cursor

ice_screenshot_20151226-204234

MongoDB Tutorial [ Finding one Document ]

File Name :find_one.js

mongo = new Mongo(“localhost”);
wordsDB = mongo.getDB(“words”);
wordsColl = wordsDB.getCollection(“word_stats”);
word = wordsColl.findOne();
print(“Single Document: “);
printjson(word);

ice_screenshot_20151226-202837

MongoDB Tutorial [ Creating the Example ]

Download Example

After this Post All Practical is Regarding this Example.

ice_screenshot_20151226-111751

Count the Words from Database

ice_screenshot_20151226-201532

 

Translate SQL query to MongoDB syntax

http://www.querymongo.com/

MongoDB Tutorial [ Delete Collection ]

Purpose : To delete the Collection.

collection_delete.js

mongo=new Mongo(“localhost”);
myDB=mongo.getDB(“newDB”);
collections=myDB.getCollectionNames();
print(“Initial Collections: “);
printjson(collections);

collection=myDB.getCollection(“newCollectionA”);
collection.drop();
print(“After Deleting newCollectionA : “);
collections=myDB.getCollectionNames();
printjson(collections);

collection=myDB.getCollection(“newCollectionB”);
collection.drop();
print(“After Deleting newCollectionB : “);
collections=myDB.getCollectionNames();
printjson(collections);

ice_screenshot_20151210-142542

 

 

MongoDB Tutorial [ show database states ]

Purpose : To show how to display the statistics for a particular database.

db_stats.js

mongo=new Mongo(“localhost”);
myDB=mongo.getDB(“admin”);
stats=myDB.stats();
printjson(stats);

ice_screenshot_20151210-141342

MongoDB Tutorial [ Create new Collections ]

Purpose : How to create new Collection.

collection_create.js

mongo=new Mongo(“localhost”);
newDB=mongo.getDB(“newDB”);
collections=newDB.getCollectionNames();
print(“Initial Collections: “);
printjson(collections);
newDB.createCollection(“newCollectionA”);
newDB.createCollection(“newCollectionB”);
print(“After Collection Creation:”);
collections=newDB.getCollectionNames();
printjson(collections);

ice_screenshot_20151210-141926

MongoDB Tutorial [ Delete Database ]

Purpose : To delete the database.

Step 1: Create file with name db_delete.js

mongo=new Mongo(“localhost”);
myDB=mongo.getDB(“newDB”);
myDB.dropDatabase();

ice_screenshot_20151210-141025

MongoDB Tutorial [ Create new Database]

Purpose : how to create database and verify that it is created using shell.

Step 1:

Create file with name : db_create.js

mongo=new Mongo(“localhost”);
newDB=mongo.getDB(“newDB”);
newDB.createCollection(“newCollection”);

ice_screenshot_20151210-140530

 

MongoDB Tutorial [ Access Control on MongoDB]

Purpose: To show you how to create User and Database Administrator accounts and enable authentication on your MOngoDB server.

For create Administrator account

Step:1 Create file name :add_admin_account.js

mongo=new Mongo(“localhost”);
db=mongo.getDB(“admin”);
db.addUser({
            user:”dbadmin”,
            pwd:”test”,
            roles:[“readWriteAnyDatabase”,
                    “dbAdminAnyDatabase”,
                    “clusterAdmin”]
            });
db.addUser({
            user:”useradmin”,
            pwd:”test”,
            roles:[“userAdminAnyDatabase”]
        })

ice_screenshot_20151210-123033

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now allow only authenticate user,

ice_screenshot_20151210-125623

Create file with name : mongod_config_auth.txt

verbose=true
port=27017
dbpath=I:\mongo\
auth=true

ice_screenshot_20151210-134501

After config the file write below command in shell

ice_screenshot_20151210-134656

It will show error msg which indicate you have not permission to access user detail.

Now we add authentication using username and password using

db.auth(“<username>”,”<password>”)

ice_screenshot_20151210-134849

 

 

 

MongoDB Tutorial [ Drop user ]

 

ice_screenshot_20151210-105357

Method 2:

Create remove_user.js

mongo = new Mongo(“localhost”);
db=mongo.getDB(“test”);
db.dropUser(“testWriter”)
cur=db.system.users.find();
printjson(cur.toArray());

MongoDB Tutorial [ List all user name with detail ]

Method 1 :Using javascript file

Step 1:

Write this code in list_user.js

mongo = new Mongo(“localhost”);
db=mongo.getDB(“test”);
cur=db.system.users.find();
printjson(cur.toArray());

Write Command in console:

mongo list_user.js

 

Method  2: Using Shell script command

ice_screenshot_20151210-103410

MongoDB Tutorial [ Create user using Shell ]

Step 1:

Write this code in add_user.js file

mongo = new Mongo(“localhost”);
db = mongo.getDB(“test”)
db.addUser({
            user : “tsetAdmin” ,
            pwd : “test” ,
            roles : [“dbadmin”]
            });

db.addUser({
            user : “tsetWriter” ,
            pwd : “test” ,
            roles : [“readWrite”]
            });

db.addUser({
            user : “tsetReader” ,
            pwd : “test” ,
            roles : [“read”]
            });

db = mongo.getDB(“admin”)
db.addUser({
            user : “tsetUser”,
            userSource:”test”,
            roles:[“read”],
            otherDBRoles:{test:[“readWrite”]}
            });

Step 2:

Create running environment of mongodb. [Previous Tutorial]

Step 3:

 

ice_screenshot_20151210-102215

MongoDB tutorial [Practical 1]

Step 1:

set path using command [Tutorial 1]

Step 2:

write in shell : mongo.exe in path of bin folder

for use specific user databse write use <username>

Make one javascript file : shell_script.js

print(“Host Name”);
print(“\t”+hostname());
print(“Date”);
print(“\t”+Date());
db=connect(“localhost/admin”);
print(“Amdin Collections: “);
printjson(db.getCollectionNames());

Save in DBpath directory

Practical 1 : Show Host name , Date and Total no of collection in database

ice_screenshot_20151210-095749

Second Method to execute .js file

ice_screenshot_20151210-100235

 

 

Watch this Video Before start learn MongoDB

[ Part 4 : Connection with Robomongo] MongoDB Development

Visit Previous Post Before visit this……

Step:1  Install Robomongo from any site

Step:2 Open Robomongo

 

Click on Create

ice_screenshot_20151012-194301

You can write any name:ice_screenshot_20151012-194333  You can add Username and Password also ( optional )

ice_screenshot_20151012-194349

Click on Connect

ice_screenshot_20151012-194435

This is your Database which you already create in Previous Post.

ice_screenshot_20151012-194521

ice_screenshot_20151012-194452

[Part 3 : Usefull Information ] MongoDB Development

In NOSQL Database we use different term of normal database scenario.

Example:

Table –> Collection

Tuple/Row –> Document

Column –> Field

 

[Part 2 : Command and Basic Practical]MongoDB Development

>show dbs    It will show the default Database.

Write this Statements

>a = { title:"Arrested Development", airdate:"November 2, 2003", network:"FOX" }
> b = { title:"Stella", airdate:"June 28, 2005", network:"Comedy Central" }
> c = { title:"Modern Family", airdate:"September 23, 2009", network:"ABC" }
db.<yourdatabase name>.save( varialbe ) : This command save the data.
>db.shows.save(a)
> db.shows.save(b)
> db.shows.save(c)
db.<yourdatabase name>.find() : Show your database content.
 >db.stats()
To get stats about mongodb server type the command db.stats() in mongodb client. This will show the database name, number of collection and documents in the database. 
>db :To check current database
>use <database name> : To take as your current database
>db.<yourdatabasename>.save( { “name”:”androidtour.wordpress.com” }  )
>db.<yourtablename>.insert( { “name”:”androidtour” } )
You can also Write direct:
>db.test.save( { a: 1 } )
>db.test.find()

[Part 1: Basic] MongoDB Developement

Step 1

First install node js

create one file in nodejs folder (node js installed folder) is hello.js

var http = require(‘http’);
http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World !’);
}).listen(8080, ‘127.0.0.1’);
console.log(‘Server running at http://127.0.0.1:8080/&#8217;);

Step:2 Install MongoDB from any site

After install go to path C:\Program Files\MongoDB 2.6 Standard\bin

First Step is to set your DB path

Write command mongod.exe –dbpath write your path of your db folder

For go to mongo shell

write command mongo.exe in bin directory.

Note: Make sure all CMD must be open otherwise it will lost Connection.

Blog at WordPress.com.

Up ↑