mongoTemplate is not able to find collection and throws exception
<p>root-context.xml:</p>

<pre><code>&lt;mongo:mongo id="mongo" host="localhost" port="27017" /&gt;
&lt;mongo:db-factory dbname="meraComputer" mongo-ref="mongo" /&gt;

&lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
    &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /&gt;
&lt;/bean&gt;

&lt;bean id="userDaoImpl" class="net.meraComputer.spring.daoImpl.UserDaoImpl"&gt;
    &lt;property name="mongoTemplate" ref="mongoTemplate"/&gt;
&lt;/bean&gt;
</code></pre>

<p>UserDaoImpl:</p>

<pre><code>private MongoTemplate mongoTemplate;

@Required
public void setMongoTemplate(MongoTemplate mongoTemplate) {
    this.mongoTemplate = mongoTemplate;
}

@Override
public void saveUser(UserAccount user) {
    if(mongoTemplate.collectionExists("userAccount")) {
        mongoTemplate.save(user);
    }else {
        mongoTemplate.createCollection("userAccount");
        mongoTemplate.save(user);
    }
}
</code></pre>

<p>I have userAccount collection already created while saving the first UserAccount.</p>

<pre><code>db.userAccount.find().pretty()
{
"_id" : ObjectId("568781929a0085d703e45e0e"),
"_class" : "net.meraComputer.spring.model.UserAccount",
"username" : "Test",
"password" : "$2a$10$XrDNeBw0vKTr0zEXVVLMy.N2L4wEzJ/C1lBAuvRQB6YC6EMFr3ws6",
"firstname" : "Test",
"lastname" : "Test",
"userRole" : [ ],
"roles" : [
    DBRef("role", "ADMIN")
]
</code></pre>

<p>But when I try to resister another user it simply return false <code>mongoTemplate.collectionExists("userAccount")</code> here and when it goes to the save part it throws an exception:</p>

<pre><code>org.springframework.data.mongodb.UncategorizedMongoDbException: command failed [create]: { "serverUsed" : "localhost/127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "collection already exists" , "code" : 48}; nested exception is com.mongodb.CommandResult$CommandFailure: command failed [create]: { "serverUsed" : "localhost/127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "collection already exists" , "code" : 48}
org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:78)
org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1800)
org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:388)
org.springframework.data.mongodb.core.MongoTemplate.doCreateCollection(MongoTemplate.java:1413)
</code></pre>

<p>I have tried:</p>

<pre><code>mongoTemplate.getCollectionNames() - no result.
mongoTemplate.collectionExists(UserAccount.class) - false
</code></pre>