如何使用MongoClient类从Mongo Java驱动程序调用db.Collection.stats()。

huangapple go评论87阅读模式
英文:

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;

如何通过名称获取集合,然后获取其状态,以便以下信息可用:

  1. 大小
  2. 存储大小

集合状态

似乎这个如何从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.

        &lt;dependency&gt;
			&lt;groupId&gt;org.mongodb&lt;/groupId&gt;
			&lt;artifactId&gt;mongodb-driver&lt;/artifactId&gt;
			&lt;version&gt;3.12.2&lt;/version&gt;
		&lt;/dependency&gt;

Package:

package com.mongodb.MongoClient;

How can I get collection by name then get its status so that the following information will be available:

  1. size
  2. storageSize

Collection Status

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=&quot;jcg&quot;, auth_pwd = &quot;admin@123&quot;, host_name = &quot;localhost&quot;, db_name = &quot;mongoauthdemo&quot;, db_col_name = &quot;emp&quot;, encoded_pwd = &quot;&quot;;

        /* Imp. Note -
         *      1.  Developers will need to encode the &#39;auth_user&#39; or the &#39;auth_pwd&#39; string if it contains the &lt;code&gt;:&lt;/code&gt; or the &lt;code&gt;@&lt;/code&gt; symbol. If not, the code will throw the &lt;code&gt;java.lang.IllegalArgumentException&lt;/code&gt;.
         *      2.  If the &#39;auth_user&#39; or the &#39;auth_pwd&#39; string does not contain the &lt;code&gt;:&lt;/code&gt; or the &lt;code&gt;@&lt;/code&gt; symbol, we can skip the encoding step.
         */
        try {
            encoded_pwd = URLEncoder.encode(auth_pwd, &quot;UTF-8&quot;);
        } catch (UnsupportedEncodingException ex) {
            log.error(ex);
        }

        // Mongodb connection string.
        String client_url = &quot;mongodb://&quot; + auth_user + &quot;:&quot; + encoded_pwd + &quot;@&quot; + host_name + &quot;:&quot; + port_no + &quot;/&quot; + 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&lt;Document&gt; 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(&quot;mongodb://localhost:27017/&quot;)) {
	
	MongoDatabase db = mongoClient.getDatabase(&quot;test&quot;);
	Document collStatsResults = db.runCommand(new Document(&quot;collStats&quot;, &quot;myCollection&quot;));
	System.out.println(collStatsResults.get(&quot;size&quot;));
	System.out.println(collStatsResults.get(&quot;storageSize&quot;));
}

Note the usage of the try-with-resources clause; it closes the MongoClient object after its use to release the connection related resources.

huangapple
  • 本文由 发表于 2020年4月6日 11:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61052446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定