第52章:Java操作MongoDB-[Mongo-Java-3.x]

时间:2021-06-22 16:27:10
①范例:连接数据库

package cn.mldn;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoDatabase;

public class Mongo3DemoA {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn") ;

MongoClient client = new MongoClient(uri) ;

MongoDatabase db = client.getDatabase("mldn") ;

System.out.println(db.getCollection("deptcol").count());

client.close();

}

}

②范例:数据增加

package cn.mldn;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.util.ArrayList;

import java.util.List;

public class Mongo3DemoB {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol") ;

List<Document> all = new ArrayList<Document>() ;

for (int x = 0 ; x < 100 ; x ++) {

Document doc = new Document() ;

doc.append("sid" , x) ;

doc.append("name" , "姓名 - " + x) ;

doc.append("sex" , "男") ;

all.add(doc) ;

}

col.insertMany(all);

client.close();

}

}

③范例:查询全部数据

package cn.mldn;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.util.ArrayList;

import java.util.List;

public class Mongo3DemoC {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol") ;

MongoCursor<Document> cursor = col.find().iterator() ;

while (cursor.hasNext()) {

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

}

client.close();

}

}

④范例:设置范围查询

package cn.mldn;

import com.mongodb.BasicDBObject;

import com.mongodb.DBObject;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;

public class Mongo3DemoD {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol") ;

BasicDBObject cond = new BasicDBObject() ;

cond.put("sid", new BasicDBObject("$gt",5).append("$lt",10)) ;

MongoCursor<Document> cursor = col.find(cond).iterator() ;

while (cursor.hasNext()) {

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

}

client.close();

}

}

⑤范例:模糊查询

package cn.mldn;

import com.mongodb.BasicDBObject;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.util.regex.Pattern;

public class Mongo3DemoE {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol") ;

Pattern pattern = Pattern.compile("10") ;

BasicDBObject cond = new BasicDBObject() ;

cond.put("name", new BasicDBObject("$regex",pattern).append("$options","i")) ;

MongoCursor<Document> cursor = col.find(cond).iterator() ;

while (cursor.hasNext()) {

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

}

client.close();

}

}

⑥范例:分页显示

package cn.mldn;

import com.mongodb.BasicDBObject;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.util.regex.Pattern;

public class Mongo3DemoF {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol") ;

Pattern pattern = Pattern.compile("1") ;

BasicDBObject cond = new BasicDBObject() ;

cond.put("name", new BasicDBObject("$regex",pattern).append("$options","i")) ;

MongoCursor<Document> cursor = col.find(cond).skip(5).limit(5).iterator() ;

while (cursor.hasNext()) {

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

}

client.close();

}

}

⑦范例:数据修改

package cn.mldn;

import com.mongodb.BasicDBObject;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import com.mongodb.client.result.UpdateResult;

import org.bson.Document;

import java.util.regex.Pattern;

public class Mongo3DemoG {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol");

BasicDBObject condA = new BasicDBObject("sid",0) ;  // 数据查询条件

BasicDBObject condB = new BasicDBObject("$set",new BasicDBObject("name","SuperMan")) ;

UpdateResult result = col.updateMany(condA,condB) ;

System.out.println(result.getMatchedCount());

client.close();

}

}

⑧范例:删除数据

package cn.mldn;

import com.mongodb.BasicDBObject;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

import com.mongodb.client.result.DeleteResult;

public class Mongo3DemoH {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

MongoCollection col = db.getCollection("stucol");

BasicDBObject cond = new BasicDBObject("sid",0) ;  // 数据查询条件

DeleteResult result = col.deleteOne(cond) ;

System.out.println(result.getDeletedCount());

client.close();

}

}

⑨范例:统计查询

package cn.mldn;

import com.mongodb.BasicDBObject;

import com.mongodb.MongoClient;

import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import com.mongodb.client.result.DeleteResult;

import org.bson.Document;

import java.util.ArrayList;

import java.util.List;

public class Mongo3DemoI {

public static void main(String[] args) {

MongoClientURI uri = new MongoClientURI("mongodb://hello:happy@localhost:27001/mldn");

MongoClient client = new MongoClient(uri);

MongoDatabase db = client.getDatabase("mldn");

List<BasicDBObject> all = new ArrayList<BasicDBObject>() ;

MongoCollection col = db.getCollection("emps"); // 这个集合有数据

BasicDBObject cond = new BasicDBObject("$group", new BasicDBObject("_id", "$sex").append("count", new BasicDBObject("$sum", 1)).append("avg", new BasicDBObject("$avg", "$salary")));

all.add(cond) ; // 保存条件

MongoCursor<Document> cursor = col.aggregate(all).iterator() ;

while(cursor.hasNext()) {

Document doc = cursor.next() ;

System.out.println(doc.getString("_id") + "," + doc.getInteger("count") + "," + doc.getDouble("avg"));

}

client.close();

}

}