0


Java语言链接MongoDB常用的方法

一、Java链接MongoDB

1. 导入Mongo驱动包

2. 获取Mongo链接对象

  1. MongoClient mc = new MongoClient("localhost",27017);

3. 关闭链接

  1. mc.close();

二、查看库,查看集合

1. 获取库对象

  1. MongoDatabase db = mc.getDatabase("myschool");

2. 获取库中表的集合

  1. MongoIterable<String> listCollectionNames = db.listCollectionNames();
  2. MongoCursor<String> iterator = listCollectionNames.iterator();
  3. while (iterator.hasNext()) {
  4. System.out.println(iterator.next());
  5. }

三、Java对MongoDB增删改查

1. 添加数据

a. 添加一条数据

  1. //创建对象
  2. Student s = new Student();
  3. s.setSid(1);
  4. s.setSname("王俊凯");
  5. s.setBirthday(new Date());
  6. s.setSsex("男");
  7. s.setClassid(2);
  8. //将数据转换为json格式
  9. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
  10. String json = gson.toJson(s);
  11. //获取集合对象
  12. MongoCollection<Document> collection = db.getCollection("student");
  13. //添加一条数据,将json格式转换为document对象
  14. collection.insertOne(Document.parse(json));

b. 添加多条数据

  1. //存入数据
  2. List<Document> dlist=new ArrayList<Document>();
  3. for(int i=0; i<3; i++){
  4. Student s = new Student();
  5. s.setSid(Integer.toString(i+1));
  6. s.setSname("王源");
  7. s.setBirthday(new Date());
  8. s.setSsex("男");
  9. s.setClassid(1);
  10. //将数据转换为json格式
  11. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
  12. String json = gson.toJson(s);
  13. dlist.add(Document.parse(json));
  14. }
  15. //获取集合对象
  16. MongoCollection<Document> collection = db.getCollection("student");
  17. //添加多条数据
  18. collection.insertMany(dlist);

2. 删除数据

a. 删除一条数据

  1. //获取集合对象
  2. MongoCollection<Document> collection = db.getCollection("student");
  3. Student s = new Student();
  4. s.setSid(1);
  5. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
  6. Bson bson = Document.parse(gson.toJson(s));
  7. DeleteResult deleteOne = collection.deleteOne(bson);

b. 删除多条数据

  1. //获取集合对象
  2. MongoCollection<Document> collection = db.getCollection("student");
  3. Student s = new Student();
  4. s.setSname("王源");
  5. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
  6. Bson bson = Document.parse(gson.toJson(s));
  7. DeleteResult deleteMany = collection.deleteMany(bson);

3. 修改数据

a. 修改一条数据

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. //一个条件对象
  3. Bson eq = Filters.eq("sname","易烊千玺");
  4. //要修改的数据
  5. Document doc = new Document();
  6. doc.put("$set", new Document("age",22));
  7. UpdateResult updateone = collection.updateOne(eq, doc);
  8. System.out.println(updateone);

b. 修改多条数据

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. //多条件
  3. Bson bson = Filters.and(Filters.gte("age", 20),Filters.lte("age", 40));
  4. //要修改的数据
  5. Document doc = new Document();
  6. doc.put("$set", new Document("sex","男"));
  7. UpdateResult updateMany = collection.updateMany(bson, doc);
  8. System.out.println(updateMany);

4. 查询数据

a. 全查

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. FindIterable<Document> findAll = collection.find();
  3. MongoCursor<Document> iterator = findAll.iterator();
  4. while(iterator.hasNext()){
  5. System.out.println(iterator.next());
  6. }

b. 带条件查询

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. //一个条件对象
  3. Bson eq = Filters.eq("sname","易烊千玺");
  4. FindIterable<Document> findOne = collection.find(eq);
  5. MongoCursor<Document> iterator = findOne.iterator();
  6. while(iterator.hasNext()){
  7. System.out.println(iterator.next());
  8. }

c. 模糊查询

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. //使用正则表达式进行模糊查找
  3. Bson eq = Filters.regex("sname","易");
  4. FindIterable<Document> find = collection.find(eq);
  5. MongoCursor<Document> iterator = find.iterator();
  6. while(iterator.hasNext()){
  7. System.out.println(iterator.next());
  8. }

d. 分页查询

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. //分页查询
  3. FindIterable<Document> find = collection.find().skip(2).limit(3);
  4. MongoCursor<Document> iterator = find.iterator();
  5. while(iterator.hasNext()){
  6. System.out.println(iterator.next());
  7. }

e. 排序查询

  1. MongoCollection<Document> collection = db.getCollection("student");
  2. //排序查询 1升序 -1降序
  3. Bson bson = new Document("sid",1);
  4. FindIterable<Document> find = collection.find().sort(bson);
  5. MongoCursor<Document> iterator = find.iterator();
  6. while(iterator.hasNext()){
  7. System.out.println(iterator.next());
  8. }

本文转载自: https://blog.csdn.net/m0_67942533/article/details/125749734
版权归原作者 崇令 所有, 如有侵权,请联系我们删除。

“Java语言链接MongoDB常用的方法”的评论:

还没有评论