public class MongoDBJDBC{ public static void main( String args[] ){ try{ // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient("localhost", 27017 ); // (1) // 连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); // (2) mongoDatabase.createCollection("test"); MongoCollection<Document> collection = mongoDatabase.getCollection("test"); collection.insertOne(new Document.____("name","zhangsan")); // (3) System.out.println("Connect to database successfully"); FindIterable<Document> findIterable = collection.find(); MongoCursor<Document> mongoCursor = findIterable.iterator(); // (4) while(mongoCursor.hasNext()){ System.out.println(mongoCursor.next()); } System.out.println("文档插入成功"); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } }
时间: 2024-04-01 22:33:12 浏览: 17
这是一段 Java 代码,用于连接 MongoDB 数据库,并向其中插入一条名为 "zhangsan" 的文档记录。具体来说,代码的功能如下:
1. 使用 MongoClient 类建立与 MongoDB 服务器的连接;
2. 使用 getDatabase 方法获取名为 "mycol" 的数据库;
3. 使用 insertOne 方法向 "test" 集合中插入一条文档记录,其中使用了 Document 类的构造方法,设置了文档的 "name" 字段为 "zhangsan";
4. 使用 find 方法查询 "test" 集合中的所有文档记录,并使用迭代器遍历输出。
需要注意的是,这段代码中的一些参数和方法调用需要根据具体的 MongoDB 服务器和数据库进行修改。
相关问题
补全代码public class MongoDBJDBC{ public static void main( String args[] ){ try{ //连接到mongodb服务 MongoClient mongoClient = new MongoClient(_ (1)_ ); ; // 连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); mongoDatabase._ (2)_ ("test"); MongoCollection collection = mongoDatabase.getCollection("test"); collection.insertOne(new Document0._ (3)_ ("name"," zhangsan")); System.out.printIn("Connect to database sucessfly"); Findlterable findlterable=collection.find(); MongoCursor mongoCursor = findlterable._ (4)_ ; while(mongoCursor._ (5)_ ){ System.out.println(mongoCursor.next(); } System.out.printIn("文档插入成功"); }catch(Exception e){ System.err.printIn( e.getClass0.getName0 + ": " + e.getMessage0 ); } } }
补全代码如下:
public class MongoDBJDBC{
public static void main( String args[] ){
try{
//连接到mongodb服务
MongoClient mongoClient = new MongoClient("localhost", 27017); ;
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol");
mongoDatabase.createCollection("test");
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
collection.insertOne(new Document("name"," zhangsan"));
System.out.println("Connect to database successfully");
FindIterable<Document> findIterable=collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
System.out.println("文档插入成功");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
补全代码public class Mongoconnect{ public static void main( String args[ ){ try{ MongoClient mongoClient = new_ (1)_ ; MongoDatabase mongoDatabase = mongoClient.getDatabase("mydb1"); mongoDatabase.drop0; mongoDatabase.createCollection("test1"); MongoC ollection<Document> collection =_ _(2)_ ; Document document1 = new Document); document1.append("_ id", "1"); (3)___ ; document1.append("sex", "man"); (4)_ ; List<Document> documents = new ArrayList <Document>0; documents.add(document1); collection.insertMany( (5)_ ); Findlterable <Document> iter =_ (6)_; iter.forEach(new Consumer <Document>0 { @Override public void accept(Document document) { System.out.printIn(document.toJson0); }); Document doc =_ (7)_ ; collection._ (8)_ ; }catch (Exception e) { System.er.println( e.getClass0.getName0 + ": " + e.getMessage0 ); }
public class Mongoconnect{
public static void main(String args[]) {
try{
MongoClient mongoClient = new MongoClient(); // (1) 创建MongoDB客户端
MongoDatabase mongoDatabase = mongoClient.getDatabase("mydb1");
mongoDatabase.drop();
mongoDatabase.createCollection("test1");
MongoCollection<Document> collection = mongoDatabase.getCollection("test1"); // (2) 获取集合
Document document1 = new Document();
document1.append("_id", "1"); // (3) 添加字段
document1.append("name", "John");
document1.append("sex", "man"); // (4) 添加字段
List<Document> documents = new ArrayList<Document>();
documents.add(document1);
collection.insertMany(documents); // (5) 插入文档
FindIterable<Document> iter = collection.find(); // (6) 查询集合中所有文档
iter.forEach(new Consumer<Document>() {
@Override
public void accept(Document document) {
System.out.println(document.toJson());
}
});
Document doc = collection.find(eq("_id", "1")).first(); // (7) 查询指定文档
collection.deleteMany(eq("_id", "1")); // (8) 删除文档
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}