java Mongodb增删改查 java连接Mongodb实现增删改查
lhh学bg 人气:01.创建maven项目
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.4.1</version> </dependency>
2.编写代码
1.查询所有
package com.czxy.mongodb; import com.alibaba.fastjson.JSON; import com.mongodb.*; import java.util.List; import java.util.Set; public class Find { public static void main(String[] args) { //客户端链接 MongoClient mongodbClint = new MongoClient("localhost", 27017); // 获取所有的数据库 List<String> databaseNames = mongodbClint.getDatabaseNames(); for (String databaseName : databaseNames) { System.out.println("数据库名称 "+databaseName); } // 连接指定的数据库 DB db = mongodbClint.getDB("text"); //获取当前数据库下的所有集合名称 Set<String> collectionNames = db.getCollectionNames(); for (String dbname : collectionNames) { System.out.println("集合名称 "+dbname); } // 连接指定的集合 DBCollection collection = db.getCollection("stus"); // 数据集合信息 DBCursor dbObjects = collection.find(); while (dbObjects.hasNext()){ //读取数据 DBObject next = dbObjects.next(); // json 格式转换 Stus parse = JSON.parseObject(next.toString(), Stus.class); // 数据输出 System.out.println(parse); } } }
2.添加数据
package com.czxy.mongodb; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import java.util.HashMap; import java.util.Map; public class Insert { public static void main(String[] args) { // 获取连接 MongoClient mongodbClint = new MongoClient("localhost", 27017); // 连接数据库 MongoDatabase database = mongodbClint.getDatabase("text"); // 获取对象 MongoCollection<Document> stus = database.getCollection("stus"); Map<String, Object> map=new HashMap(); map.put("name", "诸葛亮"); map.put("sex", "男"); map.put("age", 35.0); map.put("address", "南阳"); Document document = new Document(map); System.out.println(document); stus.insertOne(document); } }
3.删除
package com.czxy.mongodb; import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; /** * 删除 */ public class Delete { public static void main(String[] args) { //获取连接 MongoClient location = new MongoClient("localhost", 27017); // 连接数据库 MongoDatabase database = location.getDatabase("text"); //获取集合封账对象 MongoCollection<Document> collection = database.getCollection("stus"); // 删除的条件 BasicDBObject dbObject = new BasicDBObject("name", "诸葛亮"); // 删除符合条件的第一个 collection.deleteOne(dbObject); // 删除符合条件的所有 // collection.deleteMany(new BasicDBObject("name","lisi")); } }
4.修改
package com.czxy.mongodb; import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import java.awt.*; public class Update { public static void main(String[] args) { // 获取连接 MongoClient mongoClient = new MongoClient(); // 连接数据库 MongoDatabase database = mongoClient.getDatabase("text"); // 得到集合封装队象 MongoCollection<Document> stuo = database.getCollection("stus"); // 查询条件 BasicDBObject upFind = new BasicDBObject("name", "wangwu"); // 修改的值 BasicDBObject upNew = new BasicDBObject("$set", new BasicDBObject("address", "高老庄")); // 修改符合条件的第一个值 stuo.updateOne(upFind, upNew); //修改符合条件的全部 // stuo.updateMany(upFind, upNew); } }
5.条件查询
package com.czxy.mongodb; import com.alibaba.fastjson.JSON; import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class FindEq { public static void main(String[] args) { // 获取连接 MongoClient mongoClient = new MongoClient(); // 连接数据库 MongoDatabase database = mongoClient.getDatabase("text"); // 得到集合封装队象 MongoCollection<Document> stus = database.getCollection("stus"); // 查询条件 BasicDBObject find = new BasicDBObject("name", "wangwu"); // 得到查询结果 FindIterable<Document> results = stus.find(find); // 集合遍历 for (Document result : results) { // 格式转换 String s = JSON.toJSONString(result); // 查询结果输出 System.out.println(JSON.parseObject(s,Stus.class)); } } }
加载全部内容