mongodb的java驱动包,MongoDB 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();

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.

解决方案

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 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.

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.

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);

Is above code correct was of creating collection or database.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中操作MongoDB数据库,需要使用MongoDBJava驱动程序。以下是使用Java操作MongoDB数据库的基本步骤: 1. 导入MongoDBJava驱动程序。 2. 创建MongoClient实例,该实例表示MongoDB数据库服务器的连接。 3. 获取MongoDatabase实例,该实例表示MongoDB数据库。 4. 获取MongoCollection实例,该实例表示MongoDB集合。 5. 使用MongoCollection实例执行CRUD操作。 下面是一个示例代码,演示如何使用Java操作MongoDB数据库: ```java import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class MongoDBExample { public static void main(String[] args) { // 创建MongoClient实例,连接MongoDB服务器 MongoClient mongoClient = new MongoClient("localhost", 27017); // 获取MongoDatabase实例,连接到指定的数据库 MongoDatabase database = mongoClient.getDatabase("myDatabase"); // 获取MongoCollection实例,连接到指定的集合 MongoCollection<Document> collection = database.getCollection("myCollection"); // 插入文档 Document document = new Document("name", "John Doe") .append("age", 30) .append("gender", "male"); collection.insertOne(document); // 查询文档 Document query = new Document("name", "John Doe"); Document result = collection.find(query).first(); System.out.println(result); // 更新文档 Document filter = new Document("name", "John Doe"); Document update = new Document("$set", new Document("age", 31)); collection.updateOne(filter, update); // 删除文档 collection.deleteOne(filter); // 关闭MongoClient实例 mongoClient.close(); } } ``` 在上面的示例代码中,我们使用MongoDBJava驱动程序来连接到MongoDB数据库,获取MongoDB集合,并执行插入、查询、更新和删除操作。 需要注意的是,由于MongoDBJava驱动程序含在Java SDK中,因此需要手动导入MongoDBJava驱动程序库。例如,如果使用Maven构建Java项目,则需要在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.7</version> </dependency> ``` 这样就可以使用MongoDBJava驱动程序来连接、查询和操作MongoDB数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值