MongoDB Java驱动程序创建数据库和集合 [英] MongoDB Java Driver creating Database and Collection

查看:193
本文介绍了MongoDB Java驱动程序创建数据库和集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试如何创建数据库和集合mongo java驱动程序.

i was testing how to create database and collection mongo java driver.

  MongoClient client = new MongoClient("localhost",27017);
        DB db = client.getDB("ow");
        DBCollection collection = db.getCollection("documents");
        collection.save(new BasicDBObject("_id",1));
        collection.remove(new BasicDBObject("_id",1));
        boolean result = db.collectionExists("documents");
        assertTrue(result);
        assertNotNull(collection);
        client.close();

我希望对DB对象使用createCollection方法,但是发现除非插入第一个文档,否则它不会创建数据库/集合.

I would prefer to use createCollection method on the DB object, but found that it does not create database / collection unless the first document is inserted.

我的问题是这种理解正确吗?以上代码是否正确是创建集合或数据库.

My question is is this understanding correct ? Is above code correct was of creating collection or database.

推荐答案

首选在DB对象上使用createCollection方法,但发现除非第一个方法创建第一个数据库/集合,否则它不会创建数据库/集合 文档已插入.

prefer to use createCollection method on the DB object, but found that it does not create database / collection unless the first document is inserted.

当第一个文档保存到集合中时,

MongoDB创建一个集合implicitly. createCollection()方法仅显式创建一个集合,并且仅如果个选项对象作为参数传递给它.

MongoDB creates a collection implicitly when the first document is saved into a collection. The createCollection() method explicitly creates a collection only and only if an options object is passed to it as an argument.

现在这很有意义. options参数可以接受一个或多个参数来决定我们要创建的集合的特征,例如cappedautoIndexIdsizeusePowerOf2Sizesmax no. of documents.

Now this makes sense. The options parameter can take in one or more arguments to decide the characteristics of the collection we want to create such as capped,autoIndexId,size,usePowerOf2Sizes,max no. of documents.

如果我们未指定这些选项中的任何一个,则默认行为优先,即,在第一次插入时使用默认设置懒惰地创建一个集合.

If we do not specify any of these options, the default behavior would take precedence, i.e create a collection lazily whenever the first insert is made, with default settings.

因此,如果我们要定义其特征的集合,则可以将这些特征作为DBObject传递给createCollections()方法,然后将创建我们的集合.以下是如何传递选项的示例.

So if we want a collection whose characteristics we are going to define, then we can pass these characteristics as a DBObject to the createCollections() method and our collection would be created. Below is an example of how to pass the options.

BasicDBObject options =  new BasicDBObject();
options.put("size", 12121212);
db.createCollection("hello", options);

上面的代码是否正确,是有关创建集合或数据库的.

是的.它允许mongodb为您的集合应用default configuration.除非您要设定 新集合的maxsizeautoIndexIdcappedusePowerOf2Sizes属性,很好.

Yes. It allows mongodb to apply the default configuration for your collection. Unless you want to set the max,size,autoIndexId,capped,usePowerOf2Sizes properties for your new collection, this is fine.

引用: http://docs. mongodb.org/manual/reference/method/db.createCollection/

这篇关于MongoDB Java驱动程序创建数据库和集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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