您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java连接MongoDB的常用方法实例分析

2024/12/3 1:44:12发布8次查看
一、java链接mongodb1. 导入mongo驱动包
2. 获取mongo链接对象
mongoclient mc = new mongoclient("localhost",27017);
3. 关闭链接
mc.close();
二、查看库,查看集合1. 获取库对象
mongodatabase db = mc.getdatabase("myschool");
2. 获取库中表的集合
mongoiterable<string> listcollectionnames = db.listcollectionnames(); mongocursor<string> iterator = listcollectionnames.iterator(); while (iterator.hasnext()) { system.out.println(iterator.next()); }
三、java对mongodb增删改查1. 添加数据a. 添加一条数据
//创建对象student s = new student();s.setsid(1);s.setsname("王俊凯");s.setbirthday(new date());s.setssex("男");s.setclassid(2); //将数据转换为json格式gson gson = new gsonbuilder().setdateformat("yyyy-mm-dd").create();string json = gson.tojson(s); //获取集合对象mongocollection<document> collection = db.getcollection("student"); //添加一条数据,将json格式转换为document对象collection.insertone(document.parse(json));
b. 添加多条数据
//存入数据list<document> dlist=new arraylist<document>(); for(int i=0; i<3; i++){ student s = new student(); s.setsid(integer.tostring(i+1)); s.setsname("王源"); s.setbirthday(new date()); s.setssex("男"); s.setclassid(1); //将数据转换为json格式 gson gson = new gsonbuilder().setdateformat("yyyy-mm-dd").create(); string json = gson.tojson(s); dlist.add(document.parse(json));} //获取集合对象mongocollection<document> collection = db.getcollection("student"); //添加多条数据collection.insertmany(dlist);
2. 删除数据a. 删除一条数据
//获取集合对象mongocollection<document> collection = db.getcollection("student"); student s = new student();s.setsid(1); gson gson = new gsonbuilder().setdateformat("yyyy-mm-dd").create();bson bson = document.parse(gson.tojson(s)); deleteresult deleteone = collection.deleteone(bson);
b. 删除多条数据
//获取集合对象mongocollection<document> collection = db.getcollection("student"); student s = new student();s.setsname("王源"); gson gson = new gsonbuilder().setdateformat("yyyy-mm-dd").create();bson bson = document.parse(gson.tojson(s)); deleteresult deletemany = collection.deletemany(bson);
3. 修改数据a. 修改一条数据
mongocollection<document> collection = db.getcollection("student"); //一个条件对象bson eq = filters.eq("sname","易烊千玺"); //要修改的数据document doc = new document();doc.put("$set", new document("age",22));updateresult updateone = collection.updateone(eq, doc);system.out.println(updateone);
b. 修改多条数据
mongocollection<document> collection = db.getcollection("student"); //多条件bson bson = filters.and(filters.gte("age", 20),filters.lte("age", 40)); //要修改的数据document doc = new document(); doc.put("$set", new document("sex","男"));updateresult updatemany = collection.updatemany(bson, doc);system.out.println(updatemany);
4. 查询数据a. 全查
mongocollection<document> collection = db.getcollection("student"); finditerable<document> findall = collection.find(); mongocursor<document> iterator = findall.iterator(); while(iterator.hasnext()){ system.out.println(iterator.next()); }
b. 带条件查询
mongocollection<document> collection = db.getcollection("student"); //一个条件对象bson eq = filters.eq("sname","易烊千玺"); finditerable<document> findone = collection.find(eq); mongocursor<document> iterator = findone.iterator(); while(iterator.hasnext()){ system.out.println(iterator.next()); }
c. 模糊查询
mongocollection<document> collection = db.getcollection("student"); //使用正则表达式进行模糊查找bson eq = filters.regex("sname","易"); finditerable<document> find = collection.find(eq); mongocursor<document> iterator = find.iterator(); while(iterator.hasnext()){ system.out.println(iterator.next()); }
d. 分页查询
mongocollection<document> collection = db.getcollection("student"); //分页查询finditerable<document> find = collection.find().skip(2).limit(3); mongocursor<document> iterator = find.iterator(); while(iterator.hasnext()){ system.out.println(iterator.next()); }
e. 排序查询
mongocollection<document> collection = db.getcollection("student"); //排序查询 1升序 -1降序bson bson = new document("sid",1);finditerable<document> find = collection.find().sort(bson); mongocursor<document> iterator = find.iterator(); while(iterator.hasnext()){ system.out.println(iterator.next()); }
以上就是java连接mongodb的常用方法实例分析的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product