[JAVA-1300] Update Java API Docs getCollection(java.lang.String) Created: 01/Jul/14  Updated: 30/Jan/15  Resolved: 21/Aug/14

Status: Closed
Project: Java Driver
Component/s: API, Documentation
Affects Version/s: None
Fix Version/s: 2.13.0, 3.0.0

Type: Improvement Priority: Minor - P4
Reporter: Eoin Brazil Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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



 Comments   
Comment by Githook User [ 30/Jan/15 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Remove erroneous JavaDoc

JAVA-1300
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/b148d7bff99488c8d11de519fa3deb5bfd8b6119

Comment by Ross Lawley [ 21/Aug/14 ]

Thanks eoin.brazil

Comment by Githook User [ 21/Aug/14 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Remove erroneous JavaDoc

JAVA-1300
Branch: 3.0.x
https://github.com/mongodb/mongo-java-driver/commit/b148d7bff99488c8d11de519fa3deb5bfd8b6119

Comment by Githook User [ 21/Aug/14 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Remove erroneous JavaDoc

JAVA-1300
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/8a34cdc0d3fee95e67a8f23d24f86feac0e93a01

Generated at Thu Feb 08 08:54:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.