title: Query failed with error code 13 and error message 'not authorized on [db] to execute command { find:
body: <p>I am using the following</p>
  
  <p>Mongo 3.2.7
  Spring 4.3.1 release</p>
  
  <p>I have created following user with the below privileges.</p>
  
  <pre><code>&gt; use admin
  switched to db admin
  &gt; db.getUser("UserAdminNew")
  {
          "_id" : "admin.UserAdminNew",
          "user" : "UserAdminNew",
          "db" : "admin",
          "roles" : [
                  {
                          "role" : "readWrite",
                          "db" : "admin"
                  },
                  {
                          "role" : "root",
                          "db" : "admin"
                  },
                  {
                          "role" : "readWrite",
                          "db" : "NEWTest"
                  }
          ]
  }
  </code></pre>
  
  <p>In DB NEWTest I have the below mentioned collections</p>
  
  <pre><code>&gt; use NEWTest
  switched to db NEWTest
  &gt; show collections
  Friends
  users
  usersD
  </code></pre>
  
  <p>If I am inserting  &amp; trying to find the document in to usersD from console it works fine.</p>
  
  <p>where as when I am trying to do the same from spring.Even if I am able to insert the document the find fails with the following error stack.</p>
  
  <pre><code>    1. user : UserD [id=578deb8a0c58602128ead56a, username=Dhara, password=password_000]
  1.1. user : UserD [id=578deb8a0c58602128ead56b, username=Subrat Dash, password=password_111]
  query1 - Query: { "username" : "Dhara"}, Fields: null, Sort: null
  Exception in thread "main" org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 13 and error message 'not authorized on NEWTest to execute command { find: "usersD", filter: { username: "Dhara" }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on NEWTest to execute command { find: "usersD", filter: { username: "Dhara" }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017
      at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
      at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2114)
      at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1904)
      at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1712)
      at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:586)
      at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:581)
      at com.mydhara.core.App.main(App.java:60)
  Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on NEWTest to execute command { find: "usersD", filter: { username: "Dhara" }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017
      at com.mongodb.operation.FindOperation$1.call(FindOperation.java:492)
      at com.mongodb.operation.FindOperation$1.call(FindOperation.java:482)
      at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
      at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
      at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
      at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
      at com.mongodb.Mongo.execute(Mongo.java:772)
      at com.mongodb.Mongo$2.execute(Mongo.java:759)
      at com.mongodb.DBCollection.findOne(DBCollection.java:777)
      at com.mongodb.DBCollection.findOne(DBCollection.java:747)
      at com.mongodb.DBCollection.findOne(DBCollection.java:694)
      at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2143)
      at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2127)
      at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1901)
      ... 4 more
  </code></pre>
  
  <p><strong>App.java</strong></p>
  
  <pre><code>package com.mydhara.core;
  
  import java.util.List;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.context.support.GenericXmlApplicationContext;
  import org.springframework.data.mongodb.core.MongoOperations;
  import org.springframework.data.mongodb.core.convert.QueryMapper;
  import org.springframework.data.mongodb.core.query.BasicQuery;
  import org.springframework.data.mongodb.core.query.Criteria;
  import org.springframework.data.mongodb.core.query.Query;
  import org.springframework.data.mongodb.core.query.Update;
  
  import com.mydhara.config.SpringMongoConfig;
  import com.mydhara.config.SpringMongoConfig1;
  import com.mydhara.model.Friends;
  import com.mydhara.model.UserD;
  
  import org.springframework.context.support.GenericXmlApplicationContext;
  
  public class App {
  
  
  
      public static void main(String[] args) {
  
          // For XML
          ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
  
          // For Annotation
          //ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig1.class);
          MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
  
  
          UserD user = new UserD("Dhara", "password_000");
  
          // save
          mongoOperation.save(user);
  
          UserD user1 = new UserD("Subrat Dash", "password_111");
  
          // save
          mongoOperation.save(user1);
  
          // now user object got the created id.
          System.out.println("1. user : " + user);
  
          System.out.println("1.1. user : " + user1);
  
          // query to search user
          Query searchUserQuery = new Query(Criteria.where("username").is("Dhara"));
        //    BasicQuery searchUserQuery = new BasicQuery("{ username:'Dhara'}");
  
          System.out.println("query1 - " + searchUserQuery.toString());
  
          // find the saved user again.
          //UserD savedUser =mongoOperation.findOne(username:'Dhara', UserD.class);
  
          UserD savedUser = mongoOperation.findOne(searchUserQuery, UserD.class);
          System.out.println("2. find - savedUser : " + savedUser);
  
          // update password
          mongoOperation.updateFirst(searchUserQuery, Update.update("password", "new password"),
                  UserD.class);
  
          // find the updated user object
          UserD updatedUser = mongoOperation.findOne(
                  new Query(Criteria.where("username").is("Dhara")), UserD.class);
  
          System.out.println("3. updatedUser : " + updatedUser);
  
          // delete
      //  mongoOperation.remove(searchUserQuery, User.class);
  
          // List, it should be empty now. but added subrat so 1 
          List&lt;UserD&gt; listUser = mongoOperation.findAll(UserD.class);
  
  
          //rnd
          String[] originalId =new String[4];
          int i=0;
          for(UserD userN : listUser) {
  
                originalId[i]=userN.getId();
               System.out.println("value of i-------------"+i+"--------"+originalId[i]);
                i++;
  
  
          }
  
          //adding a friend
            Friends friend = new Friends("Anjusha", "password_000" , originalId[1]);
            mongoOperation.save(friend);
            Friends friend2 = new Friends("Martin", "password_111" , originalId[1]);
            mongoOperation.save(friend2);
            Friends friend3 = new Friends("Geo George", "password_222" , originalId[0]);
            mongoOperation.save(friend3);
  
          //ends
  
  
  
  
          System.out.println("4. Number of user = " + listUser.size());
          System.out.println("5. All User = " + listUser);
  
          List&lt;Friends&gt; listFriends = mongoOperation.findAll(Friends.class);
          System.out.println("6. Number of Friends = " + listFriends.size());
          System.out.println("7. All Friends = " + listFriends);
  
          // query to search friends
                  Query searchUserQueryN = new Query(Criteria.where("userId").is(originalId[1]));             
                  List&lt;Friends&gt; savedUser1 = mongoOperation.find(searchUserQueryN, Friends.class);        
                  System.out.println("--- find - friends of  Subrat Dash : " + savedUser1);
  
  
                  Query searchUserQueryM = new Query(Criteria.where("userId").is(originalId[0]));             
                  List&lt;Friends&gt; savedUser2 = mongoOperation.find(searchUserQueryM, Friends.class);        
                  System.out.println("--- find - friends of  Dhara : " + savedUser2);
  
                  // save
                  mongoOperation.save(user);
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>springconfig.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      xsi:schemaLocation="http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/data/mongo
            http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
      &lt;mongo:mongo host="127.0.0.1" port="27017" /&gt;
      &lt;mongo:db-factory dbname="NEWTest"   username="UserAdminNew"
                    password="Dhara123" /&gt; &lt;!-- orig  NEWTest --&gt;
  
      &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
          &lt;!-- &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /&gt;
           --&gt;
  
           &lt;!-- &lt;property name="writeResultChecking" value="EXCEPTION"/&gt;
           &lt;property name="writeConcern" value="FSYNC_SAFE"/&gt; --&gt;
  
  
           &lt;constructor-arg ref="mongo" /&gt; 
          &lt;constructor-arg name="databaseName" value="NEWTest"   /&gt;
  
          &lt;!-- added to chk auth feature  --&gt;
  
      &lt;constructor-arg name="userCredentials" ref="userCredentials"/&gt; 
      &lt;/bean&gt;
  
  
       &lt;bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"&gt;
      &lt;constructor-arg name="username" value="UserAdminNew" /&gt;
      &lt;constructor-arg name="password" value="Dhara123" /&gt;
  &lt;/bean&gt;  
  
  &lt;!-- added to chk auth feature ENDS HERE   --&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Any suggestions on what might me the issue here.(As per my understanding the role readWrite is capable of doing a find &amp; i can do the same from mongo console as well but not from spring)</p>
  
  <p>Thanks In Advance</p>