Feature news

Migrating from MySQL DB to MongoDB

This is a small example on Migrating from MySQL DB to MongoDB.

DB name : blog

The above diagram shows the relationships among  tables on the database called blog. Now the task is to migrate MySQL to mongoDB . 


The above image is the structure of the blog database which is structured using mongoDB.



The custom userstore database's relationships among tables shown above.

Simple and in-complex Coustom userstore with the support of mongoDB will be delivered on or before April 30th.     


Learn more »

MySQL & WSO2 Identity Server quick setup

WSO2 identity servers are compatible with MySQL database. Today we are looking quick start guide for MySQL and how to use a MySQL database to run WSO2 identity servers.

step 1 : MySQL and PHPMyadmin installation

Step 2 : go to localhost/phpmyadmin


step 3 : create a database userstore

step 4 : Download WSO2 Identity Servers and Extract


step 5 : Download MySQL jdbc connector and Extract  after that
             mysql-connector-java-5.1.38-bin.jar move to wso2is-5.1.0/repository/components/lib

step 6 : wso2is database configuration edit wso2is-5.1.0/repository/conf/datasources/master-
             datasources.xml  (add this configuration)
         
        <datasource>
           <name>WSO2_CARBON_DB</name>
           <description>The datasource used for registry and user manager</description>
           <jndiConfig>
               <name>jdbc/WSO2CarbonDB</name>
           </jndiConfig>
           <definition type="RDBMS">
               <configuration>
                   <url>jdbc:mysql://localhost:3306/userstore</url>
                   <username>root</username>
                   <password>root</password>
                   <driverClassName>com.mysql.jdbc.Driver</driverClassName>
                   <maxActive>80</maxActive>
                   <maxWait>60000</maxWait>
                   <minIdle>5</minIdle>
                   <testOnBorrow>true</testOnBorrow>
                   <validationQuery>SELECT 1</validationQuery>
                   <validationInterval>30000</validationInterval>
               </configuration>
           </definition>
       </datasource>



step 7 : open your terminal and run wso2 IS
            cd wso2is-5.1.0/
            sh bin/wso2server.sh -Dsetup

step 8 : now check you MySQL DB

so finally your are configured.

Learn more »

Apache, MySQL ,PHPmyadmin and ,PHP Install in Ubuntu 14.04

Install apache, open terminal and type in these commands:
sudo apt-get update
sudo apt-get install apache2

Install MySQL, open terminal and type in these commands:
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

Install PHP, open terminal and type in this command.
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

Install phpMyAdmin
sudo apt-get install phpmyadmin apache2-utils

After the installation has completed, add phpmyadmin to the apache configuration.
sudo nano /etc/apache2/apache2.conf

Add the phpmyadmin config to the file.
Include /etc/phpmyadmin/apache.conf

Restart apache:
sudo service apache2 restart

Set Up the .htaccess File
sudo nano /etc/phpmyadmin/apache.conf

<Directory /usr/share/phpmyadmin>
       Options FollowSymLinks
       DirectoryIndex index.php
          AllowOverride All
         [...]

Installation finished. go to your web browser
localhost              
 













localhost/phpmyadmin














successfully installed .
Learn more »

Gsoc 2016 - Document Based NoSQL Support for WSO2 Identity Server Database

Description

WSO2 Identity Server out of the box supports LDAP, Microsoft Active Directory and Relational Databases as User Stores. With the fast growth of enterprise level NoSQL databases, it is important to support NoSQL user stores to improve the value that WSO2 Identity Server can deliver to enterprises in terms of user data management.
In order to support NoSQL, It is expected to customize or write a new User Store Manager that supports storing/retrieving data from NoSQL databases.
The database architecture has to be completely redesigned for optimizing the performance of operations. Initially, new document collections can be created for each database table referring the current relational database schema. Some of the document collections can be combined together as embedded documents or new data models should be created for performance improvement considering the following facts.  

- Document size considerations
- Complexity of data structures
- Data Consistency

The performance has to be further improved by creating appropriate indexes. 
Initially the project should be done on MongoDB 3.2  which is a Document based NoSQL database that is widely used . Test automation should be done appropriately as Unit Tests or Integration Tests for all the operations in the userstore manager. 
Based on completion, other Document based databases such as CouchDB, RavenDB can be considered for implementation.

Deliverables

  • Database Architecture Diagrams
  • Custom Userstore Manager
  • Automated Tests
  • Developer documentation
  • Articles/blog posts on using the custom userstore manager

Skills Needed

  • Java
  • Database knowledge and SQL
  • Understanding of NoSQL
  • MongoDB 3.2
  • Using MongoDB with Java 
  • Test Automation (Junit, Selenium)

Learn more »

14. MongoDB - CRUD operations - update the fileds using $set , $unset




dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes

/*  find the all document from info collection  */
> db.info.find()
{ "_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }

/*  find the document '_id' equal 'fahimfana' from info collection  */
> db.info.find({ "_id" : "fahimfana"}).pretty()
{
"_id" : "fahimfana",
"age" : 22,
"email" : {
"personal" : "mohamedfahim91@gmail.com",
"offcial" : "fahimfana91@gmail.com"
},
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
}
}

/*  update document from '_id' equal 'fahimfana'  ==> this is wrong way because erase other details  can be use $set command  */
> db.info.update({_id:"fahimfana"},{age:18})
>
> db.info.find({_id:"fahimfana"})
{ "_id" : "fahimfana", "age" : 18 }
> db.info.find()
{ "_id" : "mohamedzajith", "age" : 24, "email" : { "personal" : "mohamedzajith@uojonline.net", "offcial" : "mohamedzajith@gmail.com" }, "address" : { "street" : "campus lane", "city" : "jaffna", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "nimishan", "age" : 23, "email" : { "personal" : "nimishan.siv@gmail.com", "offcial" : "nimishan@live.com" }, "address" : { "street" : "bandarawela", "city" : "badulla", "country" : "srilanka" } }
----------------------------------------------------------------------------------------------------------------------------------
{ "_id" : "fahimfana", "age" : 18 }

> db.info.update({_id:"fahimfana"},{locatin:"Srilanka"})
> db.info.find({_id:"fahimfana"})
{ "_id" : "fahimfana", "locatin" : "Srilanka" }

/*  insert a document '_id':'fahimfana'   */
> db.info.insert({"_id" : "fahimfana", "age" : 22, "email" : { "personal" : "mohamedfahim91@gmail.com", "offcial" : "fahimfana91@gmail.com" }, "address" : { "street" : "hettuwewa", "city" : "anuradapura", "country" : "srilanka" } })

> db.info.find({ "_id" : "fahimfana"}).pretty()
{
 "_id" : "fahimfana",
 "age" : 22,
 "email" : {
  "personal" : "mohamedfahim91@gmail.com",
  "offcial" : "fahimfana91@gmail.com"
 },
 "address" : {
  "street" : "hettuwewa",
  "city" : "anuradapura",
  "country" : "srilanka"
 }
}

#  Query using $set()
Use the $set operator to replace the value of a field to the specified value. If the field does not exist, the $set operator will add the field with the specified value.

> db.info.update({_id:"fahimfana"},{$set:{age:18}})
>
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"age" : 18,
"email" : {
"personal" : "mohamedfahim91@gmail.com",
"offcial" : "fahimfana91@gmail.com"
},
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
}
}
> db.info.update({_id:"fahimfana"},{$set:{"email.business":"fahimPvt@gmail.com"}})
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
},
"age" : 18,
"email" : {
"business" : "fahimPvt@gmail.com",
"offcial" : "fahimfana91@gmail.com",
"personal" : "mohamedfahim91@gmail.com"
}
}

#  Query using $unset()
The $unset operator deletes a particular field. The specified value in the $unset expression (i.e. "" below) does not impact the operation.

> db.info.update({_id:"fahimfana"},{$unset:{"email.business":"fahimPvt@gmail.com"}})
> db.info.find({_id:"fahimfana"}).pretty()
{
"_id" : "fahimfana",
"address" : {
"street" : "hettuwewa",
"city" : "anuradapura",
"country" : "srilanka"
},
"age" : 18,
"email" : {
"offcial" : "fahimfana91@gmail.com",
"personal" : "mohamedfahim91@gmail.com"
}
}
>


Learn more »

13. MongoDB - CRUD operations - Part_III_Querying , Coursor {sort()}



dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes
>
#  uery using sort()
Specifies the order in which the query returns matching documents. You must apply sort() to the cursor before retrieving any documents from the database. {1 : ascending order  , -1 : descending order }

/* find all document from result collections and '_id' filed hide  */ 
> db.result.find({},{_id:false})
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 5, "exam type" : "Exams", "score" : 55 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
Type "it" for more

/* find all document from result collections , '_id' filed hide and score is ascending order using sort function */ 
> db.result.find({},{_id:false}).sort({score:1})
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 9, "exam type" : "Quiz", "score" : 3 }
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 8, "exam type" : "Quiz", "score" : 14 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 12, "exam type" : "Exams", "score" : 20 }
{ "student" : 12, "exam type" : "Quiz", "score" : 21 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
Type "it" for more

/* find all document from result collections , '_id' filed hide and score is descending order using sort function */ 
> db.result.find({},{_id:false}).sort({score:-1})
{ "student" : 10, "exam type" : "Quiz", "score" : 93 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 16, "exam type" : "Essay", "score" : 87 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 17, "exam type" : "Quiz", "score" : 83 }
{ "student" : 11, "exam type" : "Essay", "score" : 79 }
{ "student" : 16, "exam type" : "Exams", "score" : 77 }
{ "student" : 14, "exam type" : "Essay", "score" : 76 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 20, "exam type" : "Exams", "score" : 75 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 11, "exam type" : "Exams", "score" : 71 }
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 15, "exam type" : "Essay", "score" : 69 }
{ "student" : 19, "exam type" : "Essay", "score" : 69 }
{ "student" : 19, "exam type" : "Quiz", "score" : 67 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
Type "it" for more


/* find all document from result collections , '_id' filed hide and (score , student ) are ascending order using sort function */ 
> db.result.find({},{_id:false}).sort({score:-1,student:-1})
{ "student" : 10, "exam type" : "Quiz", "score" : 93 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 16, "exam type" : "Essay", "score" : 87 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 17, "exam type" : "Quiz", "score" : 83 }
{ "student" : 11, "exam type" : "Essay", "score" : 79 }
{ "student" : 16, "exam type" : "Exams", "score" : 77 }
{ "student" : 14, "exam type" : "Essay", "score" : 76 }
{ "student" : 20, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 11, "exam type" : "Exams", "score" : 71 }
{ "student" : 19, "exam type" : "Essay", "score" : 69 }
{ "student" : 15, "exam type" : "Essay", "score" : 69 }
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 19, "exam type" : "Quiz", "score" : 67 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }
Type "it" for more

/* find all document from result collections , '_id' filed hide and student is descending order using sort function */ 
> db.result.find({},{_id:false}).sort({student:-1})
{ "student" : 20, "exam type" : "Exams", "score" : 75 }
{ "student" : 20, "exam type" : "Essay", "score" : 27 }
{ "student" : 20, "exam type" : "Quiz", "score" : 32 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }
{ "student" : 19, "exam type" : "Essay", "score" : 69 }
{ "student" : 19, "exam type" : "Quiz", "score" : 67 }
{ "student" : 18, "exam type" : "Exams", "score" : 42 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 18, "exam type" : "Quiz", "score" : 39 }
{ "student" : 17, "exam type" : "Exams", "score" : 29 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 17, "exam type" : "Quiz", "score" : 83 }
{ "student" : 16, "exam type" : "Exams", "score" : 77 }
{ "student" : 16, "exam type" : "Essay", "score" : 87 }
{ "student" : 16, "exam type" : "Quiz", "score" : 40 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Essay", "score" : 69 }
{ "student" : 15, "exam type" : "Quiz", "score" : 36 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 14, "exam type" : "Essay", "score" : 76 }
Type "it" for more

/* find all document from name collections and '_id' is descending order using sort function */ 
> db.name.find().sort({_id:-1})
{ "_id" : "Tula Steadman", "favoriteGame" : [  "football",  "formula" ] }
{ "_id" : "Tonia Jefferis", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Sebastian Kuo", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Refugia Kitts", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Pierre Ye", "favoriteGame" : [  "pool",  "basketball" ] }
{ "_id" : "Petra Clarke", "favoriteGame" : [  "pool",  "tennis" ] }
{ "_id" : "Pearlie Luther", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Nimishan", "favoriteGame" : [  "football",  "cricket" ], "age" : 24 }
{ "_id" : "Neida Bandy", "favoriteGame" : [  "tennis",  "hokey" ] }
{ "_id" : "Lazaro Izzi", "favoriteGame" : [  "basketball",  "IPL" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [  "cricket",  "formula" ] }
{ "_id" : "Jared Toenjes", "favoriteGame" : [  "hokey",  "football" ] }
{ "_id" : "Janett Olivera", "favoriteGame" : [  "football",  "basketball" ] }
{ "_id" : "Gisele Melancon", "favoriteGame" : [  "cricket",  "tennis" ] }
{ "_id" : "Gianna Kowaleski", "favoriteGame" : [  "NFS",  "formula" ] }
{ "_id" : "Gene Smits", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Fana", "favoriteGame" : [  "IPL",  "cricket" ], "age" : 23 }
Type "it" for more


Learn more »

12. MongoDB - CRUD operations - Part_II_Querying , Coursor {skip(),limit()}


#  limit()
Use the limit() method on a cursor to specify the maximum number of documents the cursor will return

#  skip()
Call the skip() method on a cursor to control where MongoDB begins returning results. This approach may be useful in implementing “paged” results.

> db.result.find()
{ "_id" : ObjectId("538de41d2110740e7c9a5373"), "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5375"), "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "_id" : ObjectId("538de41d2110740e7c9a5376"), "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a5379"), "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "_id" : ObjectId("538de41d2110740e7c9a537a"), "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "_id" : ObjectId("538de41d2110740e7c9a537b"), "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "_id" : ObjectId("538de41d2110740e7c9a537c"), "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "_id" : ObjectId("538de41d2110740e7c9a537e"), "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "_id" : ObjectId("538de41d2110740e7c9a537f"), "student" : 5, "exam type" : "Exams", "score" : 55 }
{ "_id" : ObjectId("538de41d2110740e7c9a5380"), "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5381"), "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5382"), "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "_id" : ObjectId("538de41d2110740e7c9a5383"), "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "_id" : ObjectId("538de41d2110740e7c9a5384"), "student" : 6, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a5385"), "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "_id" : ObjectId("538de41d2110740e7c9a5386"), "student" : 7, "exam type" : "Essay", "score" : 5 }
Type "it" for more

/* hide '_id' filed  and find all documents */
> db.result.find({_id:false})
> db.result.find({},{_id:false})
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 5, "exam type" : "Exams", "score" : 55 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
Type "it" for more

/* hide '_id' filed  and display the first 5 documents */
> db.result.find({},{_id:false}).limit(5)
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }

/* hide '_id' filed  and skip first 3 documents */
> db.result.find({},{_id:false}).skip(3)
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "student" : 5, "exam type" : "Exams", "score" : 55 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 8, "exam type" : "Exams", "score" : 40 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
Type "it" for more

/* hide '_id' filed  and skip first 3 document  display the 5 documents (4,5,6,7,8)*/
> db.result.find({},{_id:false}).skip(3).limit(5)
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }

/* hide '_id' filed  and select 'score' lessthan equal 20 */
> db.result.find({score:{$lte:20}},{_id:false})
{ "student" : 3, "exam type" : "Exams", "score" : 12 }
{ "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "student" : 6, "exam type" : "Exams", "score" : 11 }
{ "student" : 6, "exam type" : "Essay", "score" : 5 }
{ "student" : 7, "exam type" : "Exams", "score" : 3 }
{ "student" : 7, "exam type" : "Essay", "score" : 5 }
{ "student" : 8, "exam type" : "Quiz", "score" : 14 }
{ "student" : 9, "exam type" : "Quiz", "score" : 3 }
{ "student" : 12, "exam type" : "Exams", "score" : 20 }
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }

/* hide '_id' filed  , select 'score' lessthan equal 20 and count all document */
> db.result.find({score:{$lte:20}},{_id:false}).count()
18

/* hide '_id' filed  , select 'score' lessthan equal 20 and skip 10 documents */
> db.result.find({score:{$lte:20}},{_id:false}).skip(10)
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }
{ "student" : 14, "exam type" : "Exams", "score" : 4 }
{ "student" : 15, "exam type" : "Exams", "score" : 4 }
{ "student" : 17, "exam type" : "Essay", "score" : 12 }
{ "student" : 18, "exam type" : "Essay", "score" : 3 }
{ "student" : 19, "exam type" : "Exams", "score" : 4 }

/* hide '_id' filed  , select 'score' lessthan equal 20 and skip 10 documents  limit 3 documents*/
> db.result.find({score:{$lte:20}},{_id:false}).skip(10).limit(3)
{ "student" : 12, "exam type" : "Essay", "score" : 3 }
{ "student" : 13, "exam type" : "Essay", "score" : 18 }
{ "student" : 13, "exam type" : "Quiz", "score" : 5 }


Learn more »

11. MongoDB - CRUD operations - Part_I_Querying , Coursor {count()}


dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
info
name
result
system.indexes

#  count()
Counts the number of documents in a collection. Returns a document that contains this count and as well as the command status

/* count all document */
> db.name.find().count()
31
> db.name.count()
31
> db.name.find({}).count()
31
> db.name.count({})
31
/* count the document  '_id' value start with first letter 'A'  */   
> db.name.find({_id:{$regex:"^A"}}).count()
2
> db.name.count({_id:{$regex:"^A"}})
2
> db.name.find({_id:{$regex:"^A"}})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }

/* find the all document */   
> db.name.find({})
{ "_id" : "Tonia Jefferis", "favoriteGame" : [  "basketball",  "tennis" ] }
{ "_id" : "Daphine Chilson", "favoriteGame" : [  "hokey",  "basketball" ] }
{ "_id" : "Tiffani Trapp", "favoriteGame" : [  "cricket",  "NFS" ] }
{ "_id" : "Pearlie Luther", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Pierre Ye", "favoriteGame" : [  "pool",  "basketball" ] }
{ "_id" : "Kelvin Spurling", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Erminia Kubala", "favoriteGame" : [  "IPL",  "pool" ] }
{ "_id" : "Petra Clarke", "favoriteGame" : [  "pool",  "tennis" ] }
{ "_id" : "Bambi Geraci", "favoriteGame" : [  "IPL",  "formula" ] }
{ "_id" : "Jene Goding", "favoriteGame" : [  "football",  "cricket" ] }
{ "_id" : "Dion Spradlin", "favoriteGame" : [  "pool",  "NFS" ] }
{ "_id" : "Enola Robert", "favoriteGame" : [  "formula",  "IPL" ] }
{ "_id" : "Daina Mclendon", "favoriteGame" : [  "hokey",  "IPL" ] }
{ "_id" : "Lazaro Izzi", "favoriteGame" : [  "basketball",  "IPL" ] }
{ "_id" : "Sebastian Kuo", "favoriteGame" : [  "NFS",  "football" ] }
{ "_id" : "Jeanine Pigott", "favoriteGame" : [  "cricket",  "formula" ] }
{ "_id" : "Gene Smits", "favoriteGame" : [  "formula",  "pool" ] }
{ "_id" : "Janett Olivera", "favoriteGame" : [  "football",  "basketball" ] }
{ "_id" : "Alta Oxner", "favoriteGame" : [  "cricket",  "pool" ] }
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
Type "it" for more

/* count the document  '_id' value start with first letter 'A' and 'favoriteGame' equal 'NFS' */  
 > db.name.find({_id:{$regex:"^A"},"favoriteGame":"NFS"})
{ "_id" : "Alessandra Helton", "favoriteGame" : [  "NFS",  "football" ] }
> db.name.find({_id:{$regex:"^A"},"favoriteGame":"NFS"}).count()
1
> db.name.count({_id:{$regex:"^A"},"favoriteGame":"NFS"})
1

/* count the all document */  
> db.result.find().count()
60
> db.result..count()
60

/* count the document  'score' value graterthan equal '90'*/  
> db.result.find({score:{$gte:90}}).count()
3
> db.result.count({score:{$gte:90}})
> db.result.find({score:{$gte:90}})
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5387"), "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5390"), "student" : 10, "exam type" : "Quiz", "score" : 93 }
> db.result.find({score:{$gte:90}},{_id:false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 7, "exam type" : "Quiz", "score" : 92 }
{ "student" : 10, "exam type" : "Quiz", "score" : 93 }

/* count the document  'score' value graterthan equal '92' and 'exam type' equal 'Essay'*/  
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false}).count()
1
> db.result.count({score:{$gte:90},"exam type":"Essay"},{_id:false})
1                                                                                                                                               
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false,student:true})
{ "student" : 1 }
> db.result.find({score:{$gte:90},"exam type":"Essay"},{_id:false,student:false})
{ "exam type" : "Essay", "score" : 92 }


Learn more »