mongodb java入门:
maven工程:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>test</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>test Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>ma.glasnost.orika</groupId> <artifactId>orika-core</artifactId> <version>1.4.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.6</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.13.3</version> </dependency> </dependencies> <build> <finalName>test</finalName> </build> </project>
测试代码:
package com.test.utils; import com.mongodb.*; /** * Created by Administrator on 2016/5/19. */ public class MongoDBJDBC { public static void main( String args[] ){ try{ Long begin = System.currentTimeMillis(); // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); DB db = mongoClient.getDB("admin"); // 连接到数据库 System.out.println("Connect to database successfully"); DBCollection dbCollection = db.getCollection("order"); for (int i=2;i<10000;i++) { BasicDBObject ob = new BasicDBObject(); ob.put("a",i); ob.put("_id",i+""); dbCollection.insert(ob); } Long end = System.currentTimeMillis(); System.out.println("ms:"+(end-begin)/1000); DBObject dbObject = dbCollection.findOne(); System.out.println(dbObject.get("a")); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } }
官方文档也是for循环插入的,于是想找批量插入的api,结果没有。因为mongodb没有批量插入的概念,
以此记录下。继续学习mongodb.