mongodb java驱动_Java操作MongoDB之mongodb-driver(一)

1. mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动。

(1)通过maven仓库导入:https://mvnrepository.com/artifact/org.mongodb/mongodb-driver

例如:

org.mongodb

mongodb-driver-sync

3.11.2

2. 创建方法类

2.1 查询全部,遍历打印

packagemongodb.test;importorg.bson.Document;importcom.mongodb.BasicDBObject;importcom.mongodb.MongoClient;importcom.mongodb.client.FindIterable;importcom.mongodb.client.MongoCollection;importcom.mongodb.client.MongoDatabase;public classMongodb {/*** 查询打印全部集合*/

public static voidmongoQueryAll() {//1.创建链接

MongoClient client = new MongoClient("localhost");//2.打开数据库test

MongoDatabase db = client.getDatabase("test");//3.获取集合

MongoCollection collection = db.getCollection("stu");//4.查询获取文档集合

FindIterable documents =collection.find();//5.循环遍历

for(Document document : documents) {

System.out.println(document);

}//6.关闭连接

client.close();

}public static voidmain(String[] args) {

mongoQueryAll();

}

//打印输出stu全部数据

Document{{_id=5d7374e836a89c5a3d18b87a, name=xiaohua}}

Document{{_id=2.0, sn=002, name=xiaogang}}

Document{{_id=3.0, sn=003, name=zhangfei, job=前锋战将}}Document{{_id=5d73782736a89c5a3d18b87b, sn=004, name=xiaobingbing}}

Document{{_id=5d7396b44ec120618b2dd0cb, name=Document{{surname=李, name=世名}}, job=[皇帝, 大人物, 大丈夫, 功成名就]}}

2.2 条件查询

/*** 条件查询:如查询id为xxxx的学生所有信息*/

public static voidmongoConditionQuery() {//1.创建链接

MongoClient client = new MongoClient("localhost");//2.打开数据库test

MongoDatabase db = client.getDatabase("test");//3.获取集合

MongoCollection collection = db.getCollection("stu");//4.构建查询条件,按照name来查询

BasicDBObject stu = new BasicDBObject("name","zhangfei");//5.通过id查询记录,获取文档集合

FindIterable documents =collection.find(stu);//5.打印信息

for(Document document : documents) {

System.out.println("name:"+document.getString("name"));

System.out.println("sn:"+document.getString("sn"));

System.out.println("job:"+document.getString("job"));

}//6.关闭连接

client.close();

}public static voidmain(String[] args) {

mongoConditionQuery();

}

//执行输出

name:zhangfei

sn:003job:前锋战将

注意:当需要查询条件+判断的时候这样写,例如查询学号sn>003的学员

//查询sum大于3的学员

BasicDBObject stu = new BasicDBObject("sum",new BasicDBObject("$gt",003));

2.3 插入语句

/*** 插入语句*/

public static voidmongoInsert() {//1.创建链接

MongoClient client = new MongoClient("localhost");//2.打开数据库test

MongoDatabase db = client.getDatabase("test");//3.获取集合

MongoCollection collection = db.getCollection("stu");//4.准备插入数据

HashMap map = new HashMap();

map.put("sn","005");

map.put("name","xiaoA");

map.put("job","A工作");

map.put("sum",6);//5.将map转换成document

Document document = newDocument(map);

collection.insertOne(document);//6.关闭连接

client.close();

}//测试执行

public static voidmain(String[] args) {

mongoInsert();

}

批量插入,仅供参考:

//当需要插入多条文档的时候,循环进行单条插入当然是可以,但是效率不高,MongoDB提供了批量插入的方法List objs = new ArrayList();

objs.add(new BasicDBObject("name","user29").append("age", 30).append("sex", 1));

objs.add(new BasicDBObject("name","user30").append("age", 30).append("sex", 1));

collection.insert(objs);//这样就批量进行了插入。批量插入通过一次请求将数据传递给数据库,然后由数据库进行插入,比循环单条插入节省了每次进行请求的资源。

学习后总结,不足之处请指出,后续修改!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值