博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb测试类
阅读量:5295 次
发布时间:2019-06-14

本文共 3348 字,大约阅读时间需要 11 分钟。

public class MongoManager {

private static final String MONGO_DBNAME="local";
private static final String COLLECTION_NAME="person";
private Mongo mongo=null;
private DB db=null;
private DBCollection coll=null;
public MongoManager(){
try {
mongo=new Mongo();
db=mongo.getDB(MONGO_DBNAME);
coll=db.getCollection(COLLECTION_NAME);
System.out.println("db:"+db+",coll:"+coll);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void destory(){
if(mongo !=null){
mongo.close();
mongo=null;
db=null;
}
}
//查询单个与查询多个.
public void queryAll(){
System.out.println("queryAll start...");
DBCursor cursor = coll.find();
while(cursor.hasNext()){
System.out.println("queryAll:"+cursor.next());
}
}
//单条插入与多条插入。
public void add(){
queryAll();
System.out.println("add befor count:"+coll.count());
for(int i=0;i<5;i++){
DBObject object=new BasicDBObject();
object.put("name", "lkj"+i);
object.put("age", i);
object.put("sex", "男");
coll.save(object);
}
System.out.println("add after count:"+coll.count());
}
//单条更新与多条更新。
public void update(){
//int i=coll.update(new BasicDBObject("_id",new ObjectId("5680d3844b3ad1cd62dc8a82")), new BasicDBObject("age","22")).getN();
//System.out.println("update i:"+i);
// int j=coll.update(new BasicDBObject("_id", new ObjectId("5680d3844b3ad1cd62dc8a82")), new BasicDBObject("age", "23"), true, false).getN();
// System.out.println("update j:"+j);
BasicDBObject db=new BasicDBObject("name", "lkj");
db.append("age", 21);
db.append("sex", "男");
BasicDBObject db2=new BasicDBObject("$set", db);
BasicDBObject db3=new BasicDBObject();
Pattern p=Pattern.compile("^lkp2.*$", Pattern.CASE_INSENSITIVE);
db3.append("name", p);
//int k=coll.update(new BasicDBObject("_id", new ObjectId("5680d3844b3ad1cd62dc8a83")), db2).getN();
//前面是满足条件的,后面是要更新的内容.
int k=coll.updateMulti(db3, db2).getN();
System.out.println("update k:"+k);
}
//单条删除与多条删除
public void delete(){
//int i=coll.remove(new BasicDBObject("_id",new ObjectId("5680d3844b3ad1cd62dc8a82"))).getN();
//System.out.println("delete i:"+i);
BasicDBObject db3=new BasicDBObject();
Pattern p=Pattern.compile("^lkj.*$", Pattern.CASE_INSENSITIVE);
db3.append("name", p);
BasicDBList dblist=new BasicDBList();
dblist.add("lkj0");
dblist.add("lkj1");
BasicDBObject db=new BasicDBObject("name", new BasicDBObject("$in", dblist));
int k=coll.remove(db).getN();
System.out.println("delete k:"+k);
}
//查询单个.多个条件.模糊查询.
public void querySingle(){
// BasicDBObject db=new BasicDBObject("_id", new ObjectId("5680d2cb4b3ae0e80dc4cd16"));
// BasicDBObject db2=new BasicDBObject("age", new BasicDBObject("$lte", 26));
// db2.append("name", "lkj");
// db2.append("sex", "男");
// BasicDBList dblist=new BasicDBList();
// dblist.add(2);
// dblist.add(3);
// dblist.add(4);
BasicDBObject db=new BasicDBObject();
// db.put("age", new BasicDBObject("$in", dblist));
Pattern p=Pattern.compile("^.*1$", Pattern.CASE_INSENSITIVE);
db.append("name", p);
DBCursor cursor = coll.find(db);
while(cursor.hasNext()){
System.out.println("querySingle:"+cursor.next());
}
}
public static void main(String[] args) {
MongoManager mm=new MongoManager();
//mm.add();
//mm.update();
// mm.delete();
mm.queryAll();
//mm.querySingle();
}
}

转载于:https://www.cnblogs.com/likeju/p/5090696.html

你可能感兴趣的文章
python基础示例
查看>>
第二次作业
查看>>
Add Two Numbers
查看>>
程序集的混淆及签名
查看>>
java笔记
查看>>
MATLAB中subplot的用法
查看>>
委托小例子两个
查看>>
linux网络设备—PHY
查看>>
发送通知:Notification
查看>>
浏览器是如何工作的系列:绘制
查看>>
逻辑回归详谈
查看>>
深入理解计算机系统chapter5
查看>>
加固方案整理
查看>>
2进制,10进制,16进制,补码和移位
查看>>
Android开发之无线遥控器
查看>>
.net core webapi 中使用Swagger
查看>>
使用phpstorm进行PHP断点调试
查看>>
C# WinForm 开发游戏——小鸡快跑
查看>>
HTML基础
查看>>
RPD Volume 172 Issue 1-3 December 2016 评论01
查看>>