title: How to use "is_unique" in Codeigniter and database is an mongodb
body: <p>how to make an username as unique in validation and output is in "JSON". Actually I'm using Codeigniter to build a application and MongoDB as database. I don't know whether "is_unique" working in mongodb or not but I tried but its not working. </p>
  
  <p>Controller:</p>
  
  <p>public function create_customer()
      {</p>
  
  <pre><code> $this-&gt;load-&gt;helper('form');
  $this-&gt;load-&gt;library('form_validation');
  
  if($this-&gt;input-&gt;post('username') != $original_value) {
      $is_unique =  '|is_unique[customer.username]'
  } else {
       $is_unique =  ''
  }
  // field name, error message, validation rules
  $this-&gt;form_validation-&gt;set_rules('first_name', 'Name', 'trim|required');
  $this-&gt;form_validation-&gt;set_rules('last_name', 'Last Name', 'trim|required');
  $this-&gt;form_validation-&gt;set_rules('email_address', 'Email Address', 'trim|required|valid_email');
  $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean'.$is_unique);
  $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
  $this-&gt;form_validation-&gt;set_rules('phone_number', 'Phonenumber', 'trim|required|numeric|min_length[10]|max_length[10]');
  $this-&gt;form_validation-&gt;set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
      if($this-&gt;form_validation-&gt;run() == FALSE)
  {   
      header('Content-Type: application/json');
      echo json_encode(array('success' =&gt; 'FALSE')); 
  }   
  else
  {
      $this-&gt;load-&gt;model('general/customer');
      $new_customer[CUSTOMER_FIRST_NAME]=$this-&gt;input-&gt;post('first_name');
      $new_customer[CUSTOMER_LAST_NAME]=$this-&gt;input-&gt;post('last_name');
      $new_customer[CUSTOMER_EMAIL]=$this-&gt;input-&gt;post('email_address');
      $new_customer[CUSTOMER_USERNAME]=$this-&gt;input-&gt;post('username');
      $new_customer[CUSTOMER_PHONE]=$this-&gt;input-&gt;post('phone_number');
      $new_customer[CUSTOMER_PASSWORD]=$this-&gt;input-&gt;post('password');
      $this-&gt;customer-&gt;is_unique();
      $query = $this-&gt;customer-&gt;add($new_customer);
      header('Content-Type: application/json');
      echo json_encode(array('success' =&gt; 'TRUE'));
  }   
  } 
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public function is_unique()
  {
      $query = $this-&gt;mongo_db-&gt;select('username');
      $result = mongo_db($query);
      $count = mongo_db_num_rows($result);
      if ($count&gt;0) {
      echo 'Sorry! This Username already exists!';
      } 
      else
      {
  
       $insert = $this-&gt;mongo_db-&gt;insert(CUSTOMER, $new_customer);
       return $insert;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my code. I wanted to keep username as unique in the application.
  Please help me to complete the application.</p>