英文:
How to call db.Collection.stats() from Mongo java driver using MongoClient class
问题
我正在使用这个依赖项。
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.12.2</version>
</dependency>
包:
package com.mongodb.MongoClient;
如何通过名称获取集合,然后获取其状态,以便以下信息可用:
- 大小
- 存储大小
似乎这个如何从Mongo Java驱动程序调用db.Collection.stats()的答案使用了弃用的类package com.mongodb;
// Mongodb初始化参数。
int port_no = 27017;
String auth_user="jcg", auth_pwd = "admin@123", host_name = "localhost", db_name = "mongoauthdemo", db_col_name = "emp", encoded_pwd = "";
/* 重要说明 -
* 1. 如果开发人员需要对包含<code>: </code>或<code>@</code>符号的<code>auth_user </code>或<code>auth_pwd </code>字符串进行编码。否则,代码将抛出<code>java.lang.IllegalArgumentException </code>。
* 2. 如果<code>auth_user </code>或<code>auth_pwd </code>字符串不包含<code>: </code>或<code>@ </code>符号,则可以跳过编码步骤。
*/
try {
encoded_pwd = URLEncoder.encode(auth_pwd, "UTF-8");
} catch (UnsupportedEncodingException ex) {
log.error(ex);
}
// Mongodb连接字符串。
String client_url = "mongodb://" + auth_user + ":" + encoded_pwd + "@" + host_name + ":" + port_no + "/" + db_name;
MongoClientURI uri = new MongoClientURI(client_url);
// 使用给定的客户端uri连接到mongodb服务器。
MongoClient mongo_client = new MongoClient(uri);
// 从mongodb获取数据库。
MongoDatabase db = mongo_client.getDatabase(db_name);
// 从mongodb获取集合。
MongoCollection<Document> coll = db.getCollection(db_col_name);
英文:
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);
答案1
得分: 1
这是使用 MongoDB Java 驱动程序版本 3.12 的示例代码:
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/")) {
MongoDatabase db = mongoClient.getDatabase("test");
Document collStatsResults = db.runCommand(new Document("collStats", "myCollection"));
System.out.println(collStatsResults.get("size"));
System.out.println(collStatsResults.get("storageSize"));
}
请注意 try-with-resources
子句的用法;它会在使用后关闭 MongoClient
对象,以释放与连接相关的资源。
英文:
This is using the MongoDB Java driver version 3.12:
try(MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/")) {
MongoDatabase db = mongoClient.getDatabase("test");
Document collStatsResults = db.runCommand(new Document("collStats", "myCollection"));
System.out.println(collStatsResults.get("size"));
System.out.println(collStatsResults.get("storageSize"));
}
Note the usage of the try-with-resources
clause; it closes the MongoClient
object after its use to release the connection related resources.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论