Details
-
Improvement
-
Resolution: Done
-
Minor - P4
-
None
-
None
Description
Hi Sam,
The Java API documentation for the call getCollection(java.lang.String) are incorrect. It does not actually create the collection when called. The actual (and expected behaviour I believe) is that the collection is actually only created when the first document is inserted into it.
The behaviour can be replicated with this simple java example:
import com.mongodb.*;
|
import java.net.UnknownHostException;
|
|
|
public class JavaSimpleExample {
|
// Extra helper code
|
|
|
public static void main(String[] args) throws UnknownHostException{
|
|
|
MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017/test");
|
System.out.println(uri);
|
MongoClient client = new MongoClient(uri);
|
System.out.println(client);
|
DB db = client.getDB(uri.getDatabase());
|
System.out.println(db);
|
DBCollection c = db.getCollection("ing");
|
System.out.println(c);
|
|
|
client.close();
|
}
|
}
|
which outputs to the console:
mongodb://localhost:27017/test
|
Mongo{authority=MongoAuthority{type=Direct, serverAddresses=[localhost:27017], credentials={credentials={}}}, options=MongoOptions{description='null', connectionsPerHost=100, threadsAllowedToBlockForConnectionMultiplier=5, maxWaitTime=120000, connectTimeout=10000, socketTimeout=0, socketKeepAlive=false, autoConnectRetry=false, maxAutoConnectRetryTime=0, slaveOk=false, readPreference=primary, dbDecoderFactory=DefaultDBDecoder.DefaultFactory, dbEncoderFactory=DefaultDBEncoder.DefaultFactory, safe=false, w=0, wtimeout=0, fsync=false, j=false, socketFactory=javax.net.DefaultSocketFactory@2d35dd72, cursorFinalizerEnabled=true, writeConcern=WriteConcern { "getlasterror" : 1} / (Continue on error? false), alwaysUseMBeans=false, requiredReplicaSetName=null}}
|
test
|
ing
|
and in a connected Mongo shell to the mongod:
MongoDB shell version: 2.6.3
|
connecting to: test
|
--------------------------------------
|
Host: Eoins-MacBook-Pro.local
|
--------------------------------------
|
admin (empty)
|
local 0.078125GB
|
test (empty)
|
--------------------------------------
|
test> use test
|
switched to db test
|
test> show collections
|
test> db.ing.find()
|
test> show collections
|
test> exit
|
Thanks!
Eoin