Tuesday, May 19, 2015

The drop() Method in MongoDB

The drop() Method

MongoDB's db.collection.drop() is used to drop a collection from the database.

Syntax:

Basic syntax of drop() command is as follows
db.COLLECTION_NAME.drop()

Example:

First, check the available collections into your database mydb
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
Now drop the collection with the name mycollection
>db.mycollection.drop()
true
>
Again check the list of collections into database
>show collections
mycol
system.indexes
tutorialspoint
>
drop() method will return true, if the selected collection is dropped successfully otherwise it will return false

The createCollection() Method in MongoDB

The createCollection() Method

MongoDB db.createCollection(name, options) is used to create collection.

Syntax:

Basic syntax of createCollection() command is as follows
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and used to specify configuration of collection
ParameterTypeDescription
NameStringName of the collection to be created
OptionsDocument(Optional) Specify options about memory size and indexing

Examples:

Basic syntax of createCollection() method without options is as follows
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections
>show collections
mycollection
system.indexes
Following example shows the syntax of createCollection() method with few important options:
>db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
>
In mongodb you don't need to create collection. MongoDB creates collection automatically, when you insert some document.
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>

The dropDatabase() Method in MongoDB

The dropDatabase() Method

MongoDB db.dropDatabase() command is used to drop a existing database.

Syntax:

Basic syntax of dropDatabase() command is as follows:
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test' database

Example:

First, check the list available databases by using the command show dbs
>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB
>
If you want to delete new database <mydb>, then dropDatabase() command would be as follows:
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
Now check list of databases
>show dbs
local      0.78125GB
test       0.23012GB
>

The use Command in MongoDB

The use Command

MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it doesn't exist otherwise it will return the existing database.

Syntax:

Basic syntax of use DATABASE statement is as follows:
use DATABASE_NAME

Example:

If you want to create a database with name <mydb>, then use DATABASE statement would be as follows:
>use mydb
switched to db mydb
To check your currently selected database use the command db
>db
mydb
If you want to check your databases list, then use the command show dbs.
>show dbs
local     0.78125GB
test      0.23012GB
Your created database (mydb) is not present in list. To display database you need to insert atleast one document into it.
>db.movie.insert({"name":"tutorials point"})
>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB
In mongodb default database is test. If you didn't create any database then collections will be stored in test database.

Friday, April 24, 2015

Mongo DB as a Windows Service

1.install the mongo db software
2.create data folder under C:\Program Files\MongoDB
3. create db folder under C:\Program Files\MongoDB\data
4. create log folder under C:\Program Files\MongoDB\data
5.create conf folder under C:\Program Files\MongoDB\data

finally execute the below commend on cmd

cd C:\Program Files\MongoDB\bin

press enter then copy and paste those commends one by one

mongod.exe --dbpath "C:\Program Files\MongoDB\data\db" --port 27017

mongod.exe --dbpath "C:\Program Files\MongoDB\data\db" --logpath "C:\Program Files\MongoDB\data\log\mongod.log"

and inside the mongodb.conf file we need configure the db

dbpath = C:\Program Files\MongoDB\data\db
port = 27017
logpath = C:\Program Files\MongoDB\data\log\mongod.log

mongod -f "C:\Program Files\MongoDB\data\conf\mongodb.conf" --install --serviceName mongodb26 --serviceDisplayName "MongoDB Server Instance mongodb26" --serviceDescription "MongoDB Server Instance running on mongodb26"

Wednesday, April 15, 2015

Alert for Success of process using Struts

in Action File We need to set
if(flag==true)
            {
                req.setAttribute("msg",str);
                if(type.equals("APP"))
                    req.setAttribute("url","####.do");
                else
                    req.setAttribute("url","####.do?code="+code);                  forward=mapping.findForward("success");
            }
in Struts Config
<action path="/###" type="ActionName" parameter="Method" scope="request">
            <forward name="success" path="/Success.jsp" />
            <forward name="error" path="/Error.jsp" />
        </action>

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
  <head>
  <link rel="stylesheet" type="text/css" href="../css/<%=session.getAttribute("pageCss")%>">
  </head>
  <body class="bg">
<%String fwd=(String)request.getAttribute("fwdlink");
//System.out.println("fwd link is"+fwd);

%>

<%
if(fwd==null)
{
fwd=(String)request.getAttribute("url");
}
%>

    <script>
    var msg="<%=request.getAttribute("msg")%>";
    alert(msg);
    location.replace("<%=fwd%>");
    </script>
  </body>
 
</html>

Saturday, February 2, 2013

How to position a background image


<!DOCTYPE html>
<html>
<head>

<style>
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
margin-right:200px;
}
</style>

</head>

<body>
<h1>Hello World!</h1>
<p>W3Schools background no-repeat, set position example.</p>
<p>Now the background image is only shown once, and positioned away from the text.</p>
<p>In this example we have also added a margin on the right side, so the background image will never disturb the text.</p>
</body>

</html>

How to repeat a background image only horizontally


<!DOCTYPE html>
<html>
<head>
<style>
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
</style>
</head>

<body>
<h1>Hello World!</h1>
</body>

</html>

Bad background image

<!DOCTYPE html>
<html>
<head>
<style>
body {background-image:url('bgdesert.jpg');}
</style>
</head>

<body>
<h1>Hello World!</h1>
<p>This text is not easy to read on this background image.</p>
</body>

</html>


Set an image as the background of a page


<!DOCTYPE html>
<html>
<head>
<style>
body {background-image:url('paper.gif');}
</style>
</head>

<body>
<h1>Hello World!</h1>
</body>

</html>

et the background color of different elements


<!DOCTYPE html>
<html>
<head>
<style>
h1
{
background-color:#6495ed;
}
p
{
background-color:#e0ffff;
}
div
{
background-color:#b0c4de;
}
</style>
</head>

<body>

<h1>CSS background-color example!</h1>
<div>
This is a text inside a div element.
<p>This paragraph has its own background color.</p>
We are still in the div element.
</div>

</body>
</html>

Set the background color of a page


<!DOCTYPE html>
<html>
<head>
<style>
body
{
background-color:#b0c4de;
}
</style>
</head>

<body>

<h1>My CSS web page!</h1>
<p>Hello world! This is a W3Schools.com example.</p>

</body>
</html>