I am using this dependency.
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.12.2</version>
</dependency>
Package:
package com.mongodb.MongoClient;
How can I get collection by name then get its status so that the following information will be available:
- size
- storageSize
It seems that the answer for this How to call db.Collection.stats() from Mongo java driver uses deprecated class package com.mongodb;
// Mongodb initialization parameters.
int port_no = 27017;
String auth_user="jcg", auth_pwd = "admin@123", host_name = "localhost", db_name = "mongoauthdemo", db_col_name = "emp", encoded_pwd = "";
/* Imp. Note -
* 1. Developers will need to encode the 'auth_user' or the 'auth_pwd' string if it contains the <code>:</code> or the <code>@</code> symbol. If not, the code will throw the <code>java.lang.IllegalArgumentException</code>.
* 2. If the 'auth_user' or the 'auth_pwd' string does not contain the <code>:</code> or the <code>@</code> symbol, we can skip the encoding step.
*/
try {
encoded_pwd = URLEncoder.encode(auth_pwd, "UTF-8");
} catch (UnsupportedEncodingException ex) {
log.error(ex);
}
// Mongodb connection string.
String client_url = "mongodb://" + auth_user + ":" + encoded_pwd + "@" + host_name + ":" + port_no + "/" + db_name;
MongoClientURI uri = new MongoClientURI(client_url);
// Connecting to the mongodb server using the given client uri.
MongoClient mongo_client = new MongoClient(uri);
// Fetching the database from the mongodb.
MongoDatabase db = mongo_client.getDatabase(db_name);
// Fetching the collection from the mongodb.
MongoCollection<Document> coll = db.getCollection(db_col_name);