tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 No SQL > Mongo DB > How to add Users to MongoDB

How to add Users to MongoDB 

MongoDB is a scalable, high-performance, open source NoSQL database. It offers Ad hoc queries, Indexing, Replication, Load balancing, File storage (GridFS), Aggregation, MapReduce and Server-side JavaScript execution. This requires the library mongo-2.8.0.jar to be present in the classpath. The following example shows how to add users to the given Mongo database.

File Name  :  
com/bethecoder/tutorials/mongodb/tests/AddUserToDatabaseTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.mongodb.tests;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class AddUserToDatabaseTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    try {
      //Connect to mongoDB with given IP and PORT
      Mongo mongo = new Mongo("localhost"27017);
      
      //Get the database object
      DB database = mongo.getDB("mytestdb");
      
      String username = null;
      String password = null;
      
      for (int i = ; i < ; i ++) {
        username = "user" (i+1);
        password = "mypassword" (i+1);
        
        //Add a new user to database
        //Stores password in MD5 format
        database.addUser(username, password.toCharArray());
      }
      
      for (int i = ; i < ; i ++) {
        username = "readonlyuser" (i+1);
        password = "mypassword" (i+1);
        
        //Add a new read-only user to database
        //Stores password in MD5 format
        database.addUser(username, password.toCharArray()true);
      }
      
    catch (UnknownHostException e) {
      e.printStackTrace();
    catch (MongoException e) {
      e.printStackTrace();
    }
  }
}
   



 
  


  
bl  br