在Java中创建mongodb封顶的集合 [英] Creating a mongodb capped collection in java

查看:168
本文介绍了在Java中创建mongodb封顶的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Java代码创建一个有上限的集合.我找到了通过JavaScript创建语法的语法,但是找不到Java的示例.

I want to create a capped collection from Java code. I found the syntax for creating it through JavaScript, but could not find an example for Java.

Mongo mongo = new Mongo("127.0.0.1");
DB db = mongo.getDB("mydbid");

DBCollection collection;
if (db.collectionExists("mycollection")) {
        collection = db.getCollection("mycollection");
    } else {
        collection = /* ????? Create the collection ?????? */
    }
}

推荐答案

使用DB.createCollection操作,然后指定具有上限作为参数的DBObject.然后,您可以指定 size max ,以控制字节大小和最大文档数. MongoDB网站在封顶集合上有一个教程,它解释了所有选项,但缺少每个示例的示例驱动程序.

Use the DB.createCollection operation and then specify a DBObject that has capped as a parameter. You can then specify size and max in order to control the byte size and the maximum number of documents. The MongoDB site has a tutorial on capped collections that explains all the options, but is missing an example for each driver.

Mongo mongo = new Mongo("127.0.0.1");
DB db = mongo.getDB("mydbid");

DBCollection collection;
if (db.collectionExists("mycollection")) {
        collection = db.getCollection("mycollection");
    } else {
        DBObject options = BasicDBObjectBuilder.start().add("capped", true).add("size", 2000000000l).get();
        collection = db.createCollection("mycollection", options);
    }
}

这篇关于在Java中创建mongodb封顶的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆