[MongoDB]MongoDB与JAVA结合使用CRUD

时间:2024-01-07 20:52:14

汇总:

1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB与JAVA结合使用CRUD

使用起来也相当简单,由于MongoDB是类文件的数据库,所以其操作起来非常方便

首先下载相应的jar包,这里我直接使用Maven自动获取,以下为POM文件中的配置:

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8.2</version>

</dependency>

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>2.7.2</version>

</dependency>

一个MongoDb的jar包和Junit的jar包,即可

后面即可开始编写代码,直接在某个java类的main方法中调用即可:

public static void main(String[] args) throws Exception{

// 创建一个Mongo实例,默认使用本地"127.0.0.1", 27017

Mongo mongo1j = new Mongo();

// 使用IP+端口号创建

Mongo mongo = new Mongo("127.0.0.1", 27017);

// 得到所有数据库的名称

for(String name : mongo.getDatabaseNames()) {

System.out.println("dbName: " + name);

}

// 取得某个数据库,用于后续对数据库的操作

DB db = mongo.getDB("student");

for(String name : db.getCollectionNames()) {

System.out.println("collectionName: " + name);

}

// 取得对应的集合

DBCollection students = db.getCollection("student");

// 查询所有集合中的数据

DBCursor cur = students.find();

// 遍历集合中的所有内容

while(cur.hasNext()) {

System.out.println(cur.next());

}

System.out.println("cur.count(): " + cur.count());

System.out.println("cur.getCursorId(): " + cur.getCursorId());

System.out.println("JSON.serialize(): " + JSON.serialize(cur));

// DELETE操作

students.remove(new BasicDBObject("name", "yangjx"));

System.out.println("cur.count(): " + cur.count());

// ADD操作

DBObject student = new BasicDBObject();

student.put("name", "yangjx");

student.put("age", 55);

students.save(student);

System.out.println("cur.count(): " + cur.count());

// UPDATE操作

DBObject updateObj = new BasicDBObject();

updateObj.put("age", 30);

updateObj.put("name", "zhangGY");

updateObj.put("extra", "markHere");

students.update(new BasicDBObject("age", 30), updateObj);

System.out.println(JSON.serialize(students.find(new BasicDBObject("name", "zhangGY"))));

}

上述代码执行完成之后会打印出如下:

dbName: local

dbName: student

collectionName: colName

collectionName: student

collectionName: system.indexes

{ "_id" : { "$oid" : "555ae83a09668180fadccdeb"} , "age" : 30}

{ "_id" : { "$oid" : "555ae84009668180fadccdec"} , "name" : "zhangYC" , "age" : 29.0 , "sex" : true}

{ "_id" : { "$oid" : "555ae84709668180fadccded"} , "name" : "zhangYYQ" , "age" : 30.0 , "sex" : true}

{ "_id" : { "$oid" : "555aebdb09b332c5a188b620"} , "passwd" : "1234"}

{ "_id" : { "$oid" : "555d80d7b7a5042665b195ae"} , "name" : "yangjx" , "age" : 55}

cur.count(): 5

cur.getCursorId(): 0

JSON.serialize(): [ { "_id" : { "$oid" : "555ae83a09668180fadccdeb"} , "age" : 30} , { "_id" : { "$oid" : "555ae84009668180fadccdec"} , "name" : "zhangYC" , "age" : 29.0 , "sex" : true} , { "_id" : { "$oid" : "555ae84709668180fadccded"} , "name" : "zhangYYQ" , "age" : 30.0 , "sex" : true} , { "_id" : { "$oid" : "555aebdb09b332c5a188b620"} , "passwd" : "1234"} , { "_id" : { "$oid" : "555d80d7b7a5042665b195ae"} , "name" : "yangjx" , "age" : 55}]

cur.count(): 4

cur.count(): 5

[ { "_id" : { "$oid" : "555ae83a09668180fadccdeb"} , "age" : 30 , "name" : "zhangGY" , "extra" : "markHere"}]

参考:

http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html